Я изучаю Python / Django и пытаюсь закодировать корзину покупок. есть уменьшение_инвентаризации («Я», «сумма»), которое мне нужно вызвать в другом разделе, но оно подчеркивает это красным и говорит, что эта функция не определена, и предлагает определить ее автоматически, но я хочу назвать ее из этот класс. Как вы думаете, это происходит, потому что я пишу "пройти"? или есть метод для вызова функции из другого класса? Большое спасибо за помощь.
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