0

请问我如何解决分配前引用的错误局部变量'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()
4

1 回答 1

2

你可能有一个缩进错误。第一条if语句应该在 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()
于 2012-06-05T17:06:37.053 回答