local variable 'stockqty' referenced before assignment in django -
please how resolve error local variable 'stockqty' referenced before assignment. have save method in 1 of models searches item , return values quantity, price, cost etc keep getting error..
def save (self):
callitems=item.objects.filter(subcategory=self.itemname) callitem in callitems: stockqty=callitem.quantity #stovkqty.append(callitem.quantity) price=callitem.unitprice #price.append(callitem.unitprice) cost=callitem.unitcost #cost.append(callitem.unitcost) vat=callitem.tax.rate if self.quantity < stockqty: error complain here if self.discount==true: self.total= self.discountprice * self.quantity self.profit=self.total-(cost * self.quantity) self.salesdate=date.today() self.salestime=datetime.now() self.staff='admin' item.objects.filter(subcategory=self.itemname).update(quantity=stockqty-self.quantity) else: self.total= price * self.quantity self.profit=self.total-(cost * self.quantity) self.salesdate=date.today() self.salestime=datetime.now() self.staff='admin' item.objects.filter(subcategory=self.itemname).update(quantity=stockqty-self.quantity) super(recordsales, self).save()
you have indentation error. first if statement should inside loop, "in parallel" it. try this:
callitems=item.objects.filter(subcategory=self.itemname) callitem in callitems: stockqty=callitem.quantity #stovkqty.append(callitem.quantity) price=callitem.unitprice #price.append(callitem.unitprice) cost=callitem.unitcost #cost.append(callitem.unitcost) vat=callitem.tax.rate if self.quantity < stockqty: # error complain here if self.discount==true: self.total= self.discountprice * self.quantity self.profit=self.total-(cost * self.quantity) self.salesdate=date.today() self.salestime=datetime.now() self.staff='admin' item.objects.filter(subcategory=self.itemname).update(quantity=stockqty-self.quantity) else: self.total= price * self.quantity self.profit=self.total-(cost * self.quantity) self.salesdate=date.today() self.salestime=datetime.now() self.staff='admin' item.objects.filter(subcategory=self.itemname).update(quantity=stockqty-self.quantity) super(recordsales, self).save()
Comments
Post a Comment