局部变量';库存数量';在django中分配前引用
请说明如何解决分配前引用的错误局部变量“stockqty”。在我的一个模型中,我有一个save方法,它搜索一个项目并返回一些值,如数量、价格、成本等,但我一直收到这个错误 def保存(自我):局部变量';库存数量';在django中分配前引用,django,Django,请说明如何解决分配前引用的错误局部变量“stockqty”。在我的一个模型中,我有一个save方法,它搜索一个项目并返回一些值,如数量、价格、成本等,但我一直收到这个错误 def保存(自我): callitems=Item.objects.filter(子类别=self.ItemName) 对于callitems中的callitem: 库存数量=callitem.quantity #stovkqty.追加(callitem.数量) 价格=callitem.unitprice #price.app
callitems=Item.objects.filter(子类别=self.ItemName)
对于callitems中的callitem:
库存数量=callitem.quantity
#stovkqty.追加(callitem.数量)
价格=callitem.unitprice
#price.append(callitem.unitprice)
成本=callitem.unitcost
#附加成本(callitem.unitcost)
增值税=关税税率
如果self.quantity
您可能有缩进错误。第一个if
语句应该在for循环内,但它与for循环“并行”。试试这个:
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()
callitems=Item.objects.filter(子类别=self.ItemName)
对于callitems中的callitem:
库存数量=callitem.quantity
#stovkqty.追加(callitem.数量)
价格=callitem.unitprice
#price.append(callitem.unitprice)
成本=callitem.unitcost
#附加成本(callitem.unitcost)
增值税=关税税率
如果self.quantity
更好的是,用以下组合测试替换两个连续的if
测试:if self.quantity
(请注意,在python中,您不需要使用if object==True:
,因为非空对象求值为True,那么您只需使用if object:
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()