【问题标题】:local variable 'stockqty' referenced before assignment in django在 django 中赋值之前引用的局部变量“stockqty”
【发布时间】:2012-06-09 17:23:29
【问题描述】:

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

【问题讨论】:

    标签: django


    【解决方案1】:

    您可能有缩进错误。第一个 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()
    

    【讨论】:

    • 更好的是,用以下组合测试替换两个连续的ifs:if self.quantity &lt; stockqty and self.discount(请注意,在 python 中您不需要使用if object == True:,因为非空对象评估没错,那你就用if object:
    猜你喜欢
    • 2016-07-18
    • 2022-07-10
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    • 2020-05-05
    • 2020-08-22
    • 1970-01-01
    • 2011-05-02
    相关资源
    最近更新 更多