Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a field whose value is a calculation of other fields' values

Tags:

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.

like image 647
rechie Avatar asked Jul 13 '12 06:07

rechie


People also ask

Can calculated fields use other calculated fields?

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.

How do you create a calculated field?

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.

How do you create a calculated field in Access?

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.


Video Answer


2 Answers

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) 
like image 129
Ahsan Avatar answered Sep 27 '22 22:09

Ahsan


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 
like image 39
rechie Avatar answered Sep 27 '22 23:09

rechie