0

割り当て前に参照されたエラーローカル変数「stockqty」を解決するにはどうすればよいですか。モデルの1つに、アイテムを検索して数量、価格、コストなどの値を返すsaveメソッドがありますが、このエラーが発生し続けます。

def save(self):

    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 に答える