python - Calculating the difference between two fields within a table in Django -


i have model stores opening , closing balances of petrol station. have additional third column difference between opening , reading values.

class dailysales(models.model):     pump_names = (     ...     )     pump_name = models.charfield(max_length = 5, choices = pump_names)     opening_reading = models.integerfield()     closing_reading = models.integerfield()     gross_sales = models.integerfield(null = true)     date = models.datefield(auto_now_add= true)      def calc_gross(self):         self.gross_sales = self.opening_reading - self.closing_reading         super(dailysales,self).save() 

the calc_gross method works when i'm saving 1 instance @ time. want add multiple opening , closing values model @ once i'm using formsets.

salesform = modelformset_factory(dailysales, = 7, exclude=('date','gross_sales')) 

this logic falters. django isnt able calculate gross values each row.

exception value:      'list' object has no attribute 'calc_gross' 

i thinking since list , iterate on each , invoke calc_gross method. name of list?

is there better way this? logical use prefixes (how u assign prefix each form?)

turns out loop on formset.

if request.method == 'post': sale_form = salesform(data = request.post)

    form in sale_form:          if form.is_valid():              gross = form.save()               gross.calc_gross()             gross.save()         else:             print(sale_form.errors) 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -