1👍
✅
You can make use of or 0
to use 0
in case of a None
(or zero):
@property
def get_total_assets(self):
return (self.item_1_amount or 0) + (self.item_2_amount or 0) + (self.item_3_amount or 0) + (self.item_4_amount or 0) + (self.item_5_amount or 0)
Setting a default=0
will not work for existing records. Furthermore using multiple columns with the same "dimension" (here amounts) is often not a good idea. It might be better to make an Amount
model with a ForeignKey
to the Info
model. In that case you can link zero, one, or more Amount
s to an Info
model.
Source:stackexchange.com