繁体   English   中英

从另一个 function 中的 class 调用 function

[英]calling a function from a class inside another function

我正在学习 python/Django,并且正在尝试编写购物车代码。 有一个 reduction_inventory(self, amount) 我需要在另一个部分中调用它,但它给了我一个红色下划线,并说这个 function 没有定义并建议自动定义它,但我想从class。 你认为这是因为我写了“通行证”吗? 或者有一种方法可以从另一个 class 调用 function? 非常感谢你帮助我。

class Product(models.Model):
    code = models.CharField(max_length=10, db_index=True, unique=True)
    name = models.CharField(max_length=100, db_index=True)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    inventory = models.PositiveIntegerField(default=0)

    def increase_inventory(self, amount):
        pass

        def decrease_inventory(self, amount):
            pass

        def __str__(self):
            return 'Type: {0} Price: {1}'.format(self.name, self.price)




class Order(models.Model):
    # Status values. DO NOT EDIT
    STATUS_SHOPPING = 1
    STATUS_SUBMITTED = 2
    STATUS_CANCELED = 3
    STATUS_SENT = 4


    customer = models.ForeignKey('Customer', on_delete=models.SET_NULL, null=True)
    order_time = models.DateTimeField(auto_now=True)
    total_price = Sum(F('amount') * F('product__price'))
    status = models.IntegerField(choices=status_choices)

    @staticmethod
    def initiate(cls, customer):
        return cls.objects.create(customer=customer, status=cls.STATUS_SHOPPING)

    def add_product(self, product, amount):
        self.status = self.STATUS_SHOPPING
        print(product.id)
        if self.orderrow_set.filter(product=product).exists():
            preexisting_order_row = self.orderrow_set.filter(product=product)
            preexisting_order_row.amount += amount
            if amount == 0:
                raise ValueError(
                    print('value must be bigger than 0')
                )
            if product.inventory < amount:
                raise ValueError(
                    print('cant buy more than shops inventory')
                )
            preexisting_order_row.save()
        else:
            new_order_row = OrderRow.objects.create(
                product=product,
                order=self,
                amount=amount,
            )

    def remove_product(self, product, amount=None):
        self.status = self.STATUS_SHOPPING
        try:
            preexisting_order_row = OrderRow.objects.get(product=product, order=self)
            if preexisting_order_row.amount > 1:
                preexisting_order_row.amount -= amount
                preexisting_order_row.save()
            else:
                preexisting_order_row.delete()
        except OrderRow.DoesNotExist:
            pass

    def submit(self):
        cart = OrderRow.objects.all()
        if cart.amount > Product.inventory & Order.total_price > Customer.balance:
            self.status = self.STATUS_SHOPPING
            raise ValueError(
                print('the amount you chose is bigger than inventory or you dont have enough money'))
        else:
            self.status = self.STATUS_SUBMITTED
            Customer.balance -= Order.total_price
            decrease_inventory()
            if self.STATUS_SUBMITTED:
                print('submit completed')
                self.save()
                self.STATUS_SHOPPING

使用这个来解决这个问题。从其他 class 中的 class 调用方法

Product.decrease_inventory()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM