请问我如何解决分配前引用的错误局部变量'stockqty'。我在我的一个模型中有一个保存方法,它搜索一个项目并返回一些值,如数量、价格、成本等,但我一直收到这个错误。
def保存(自我):
callitems=Item.objects.filter(subcategory=self.ItemName)
for 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: the error complain is 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()