0

I'm a beginner with using Python. I have two values that I want to setup if/else logic with. One is the "obj.default_value" and the other is the "obj.override".

The basic summary to my problem is I want to setup an if/else statement that checks if obj.override == 'true' then obj.default_value should change to 'True'. If obj.override == 'false' then obj.default_value should change to 'False'. The final option is if obj.override == 'none' then obj.default_value should stay to the current value it was.

Better yet, if possible I would like the obj.override to negate the value of obj.default_value. True becomes false and vice versa. I want to setup a new function that overrides the default value. I hope I explained all this well and any help would be greatly appreciated.

Here's my current code for two values referenced:

    def default_value_custom(self, obj):
        color = 'green' if obj.default_value else 'red'
        return mark_safe("<span style='color:%s'>%s</span>" % (color, obj.default_value))
    default_value_custom.short_description = 'default value'


    def override_custom(self, obj):
        if obj.override == 'true':
            return mark_safe("Always <span style='color:green'>True</span>")
        elif obj.override == 'false':
            return mark_safe("Always <span style='color:red'>False</span>")
        else:
            return obj.override

Here's a screenshot as well of the admin view. enter image description here

Webalation
  • 237
  • 3
  • 13
  • It is `True` and `False` in Python; uppercase not lowercase – dawg Oct 27 '20 at 21:23
  • 2
    @dawg This is true, but the provided code seems to be checking string values, not booleans directly. – bbnumber2 Oct 27 '20 at 21:25
  • Does this answer your question? [In Python how should I test if a variable is None, True or False](https://stackoverflow.com/questions/2020598/in-python-how-should-i-test-if-a-variable-is-none-true-or-false) – Christopher Peisert Oct 27 '20 at 21:26

1 Answers1

1

Let's summarize:

  1. The condition if value: (and also while value:) tests, whether the value evaluates to boolean true. Many call such values "truthy" or "truish".

    The negated condition if not value: tests the opposite, i.e. whether the value evaluates to boolean false. Such values are called "falsy".

    Truthy is everything not falsy; typical falsy values are:

    • constants defined to be false: None and False.
    • zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
    • empty sequences and collections: '', (), [], {}, set(), range(0)

    (quoted from the docs, click for more details)

  2. Any value can be converted to boolean True or False with bool(value). In other words: truthy just means that bool(value) evalues to True.

  3. The remaining question is how to test for a constant True ( or False or None) and not just any truthy (or falsy) value. The answer is:

     if value is True: # or False/None
         ...
    

The function you were asking for is:

def overriden(value, override):
    return value if override is None else override
VPfB
  • 14,927
  • 6
  • 41
  • 75