There is a possibility that I am searching under wrong keywords, but I can't find the answer to the question:
Is there a nice one-liner to provide a default value if the variable is NoneType?
For example:
def validate_quantity(self, value):
instance = self.instance
diff = value - instance.quantity
results in error:
unsupported operand type(s) for -: 'decimal.Decimal' and 'NoneType'
I know dictionaries have a .get()
method which works like that:
smth.get('quantity', 200)
but it cannot be applied to an object instance.
This:
diff = value - (instance.quantity or 0)
does not work either because it returns 0 when instance.quantity
is None
, but also when it is []
or ""
.