class PO(models.Model) qty = models.IntegerField(null=True) cost = models.IntegerField(null=True) total = qty * cost
How will I solve total = qty * cost
above. I know it will cause an error, but have no idea of how to deal with this.
About Calculated Fields Sum is the only function available for a calculated field. A calculated field becomes a new field in the pivot table, and its calculation can use the sum of other fields.
Add a calculated field Click the PivotTable. This displays the PivotTable Tools, adding the Analyze and Design tabs. On the Analyze tab, in the Calculations group, click Fields, Items, & Sets, and then click Calculated Field. In the Name box, type a name for the field.
Select a table. Select Click to Add > Calculated Field, and then select a data type. Enter a calculation for the field, and then click OK. Type the expression yourself, or select expression elements, fields, and values to put them into the expression edit field.
You can make total
a property
field, see the docs
class PO(models.Model) qty = models.IntegerField(null=True) cost = models.IntegerField(null=True) def _get_total(self): "Returns the total" return self.qty * self.cost total = property(_get_total)
Justin Hamades answer
class PO(models.Model) qty = models.IntegerField(null=True) cost = models.IntegerField(null=True) @property def total(self): return self.qty * self.cost
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With