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
Post a Comment