Возможно, вы должны создать новый ордер, связанный с этим customer
. Что-то вроде следующего:
@classmethod
def initiate(cls, customer):
return cls.objects.create(customer=customer, status=cls.STATUS_SHOPPING)
Есть и другие проблемы с вашим кодом. Вы не можете использовать SET_NULL
, если fk не имеет значения nullable:
customer = models.ForeignKey('Customer', on_delete=models.SET_NULL, null=true)
В строке не должно быть несколько продуктов:
class OrderRow(models.Model):
product = models.ForeignKey(Product) # not many2many!
# ...
Кроме того, ваш add_product
нуждается в некотором исправлении :
def add_product(self, product, amount):
self.status = self.STATUS_SHOPPING # the instance is self + use your descriptive variables
print(product.id)
# filter only rows in the current order!
if self.orderrow_set.filter(product=product).exists():
# fix naming: this is a row, not an order
preexisting_order_row = self.orderrow_set.get(product=product)
preexisting_order_row.amount += amount # why +1, you are adding amount
preexisting_order_row.save()
else:
new_order_row = OrderRow.objects.create(
product=product,
order=self,
amount=amount,
) # create saves already