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

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -