Features: 1) Switch Promotion creation to update_or_create to handle existing records;

Fixes: 1) Ensure Promotion update logic correctly applies changes to existing entries;

Extra: 1) Code formatting adjustments to improve readability;
This commit is contained in:
Egor Pavlovich Gorbunov 2025-05-20 10:45:03 +03:00
parent d001e238f8
commit 5c98129a6f

View file

@ -209,7 +209,8 @@ def process_promotions() -> tuple[bool, str]:
product = eligible_products.order_by("?").first()
selected_products.append(product)
promotion = Promotion.objects.create(name=promotion_name, discount_percent=discount_percent, is_active=True)
promotion = Promotion.objects.update_or_create(name=promotion_name,
defaults={"discount_percent": discount_percent, "is_active": True})
promotion.products.set(selected_products)