diff --git a/core/models.py b/core/models.py index f21b11f0..6bbc12d7 100644 --- a/core/models.py +++ b/core/models.py @@ -550,7 +550,10 @@ class Order(ExportModelOperationsMixin("order"), NiceModel): def total_quantity(self) -> int: return sum([op.quantity for op in self.order_products.all()]) - def add_product(self, product_uuid: str | None = None, attributes: list = [], update_quantity: bool = True): + def add_product(self, product_uuid: str | None = None, attributes: list = None, update_quantity: bool = True): + if attributes is None: + attributes = [] + if self.status not in ["PENDING", "MOMENTAL"]: raise ValueError(_("you cannot add products to an order that is not a pending one")) try: @@ -569,7 +572,7 @@ class Order(ExportModelOperationsMixin("order"), NiceModel): order_product, is_created = OrderProduct.objects.get_or_create( product=product, order=self, - attributes=json.dumps(attributes) if attributes else {}, + attributes=json.dumps(attributes), defaults={"quantity": 1, "buy_price": product.price}, ) if not is_created and update_quantity: