Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add through model to PromotionRule - variants relation (#15562)

Iga Karbowiak4457ad079bf6
diff --git a/saleor/discount/migrations/0069_promotionrule_variants.py b/saleor/discount/migrations/0069_promotionrule_variants.py
new file mode 100644
index 0000000000..f309a0efe1
--- /dev/null
+++ b/saleor/discount/migrations/0069_promotionrule_variants.py
@@ -0,0 +1,55 @@
+# Generated by Django 3.2.24 on 2024-03-07 09:07
+
+import django.db.models.deletion
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("product", "0190_merge_20231221_1356"),
+ ("discount", "0068_promotion_start_end_date_indexes"),
+ ]
+
+ operations = [
+ migrations.SeparateDatabaseAndState(
+ state_operations=[
+ migrations.CreateModel(
+ name="PromotionRule_Variants",
+ fields=[
+ (
+ "id",
+ models.AutoField(
+ auto_created=True,
+ primary_key=True,
+ serialize=False,
+ verbose_name="ID",
+ ),
+ ),
+ (
+ "productvariant",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ to="product.productvariant",
+ ),
+ ),
+ (
+ "promotionrule",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ to="discount.promotionrule",
+ ),
+ ),
+ ],
+ ),
+ migrations.AlterField(
+ model_name="promotionrule",
+ name="variants",
+ field=models.ManyToManyField(
+ blank=True,
+ through="discount.PromotionRule_Variants",
+ to="product.ProductVariant",
+ ),
+ ),
+ ]
+ ),
+ ]
diff --git a/saleor/discount/migrations/0070_alter_promotionrule_variants_id.py b/saleor/discount/migrations/0070_alter_promotionrule_variants_id.py
new file mode 100644
index 0000000000..e02ded3ceb
--- /dev/null
+++ b/saleor/discount/migrations/0070_alter_promotionrule_variants_id.py
@@ -0,0 +1,19 @@
+# Generated by Django 3.2.24 on 2024-03-07 09:08
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("discount", "0069_promotionrule_variants"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="promotionrule_variants",
+ name="id",
+ field=models.BigAutoField(
+ editable=False, primary_key=True, serialize=False, unique=True
+ ),
+ ),
+ ]
diff --git a/saleor/discount/migrations/0071_merge_20240307_1156.py b/saleor/discount/migrations/0071_merge_20240307_1156.py
new file mode 100644
index 0000000000..4949c7646d
--- /dev/null
+++ b/saleor/discount/migrations/0071_merge_20240307_1156.py
@@ -0,0 +1,12 @@
+# Generated by Django 3.2.24 on 2024-03-07 11:56
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("discount", "0070_alter_promotionrule_variants_id"),
+ ("discount", "0070_merge_20231215_0911"),
+ ]
+
+ operations = []
diff --git a/saleor/discount/migrations/0077_merge_20240307_1217.py b/saleor/discount/migrations/0077_merge_20240307_1217.py
new file mode 100644
index 0000000000..f14b4d401e
--- /dev/null
+++ b/saleor/discount/migrations/0077_merge_20240307_1217.py
@@ -0,0 +1,12 @@
+# Generated by Django 3.2.24 on 2024-03-07 12:17
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("discount", "0071_merge_20240307_1156"),
+ ("discount", "0076_promotionrule_variants_dirty"),
+ ]
+
+ operations = []
diff --git a/saleor/discount/models.py b/saleor/discount/models.py
index a749db67cd..61461e6d9e 100644
--- a/saleor/discount/models.py
+++ b/saleor/discount/models.py
@@ -383,7 +383,9 @@ class PromotionRule(models.Model):
order_predicate = models.JSONField(
blank=True, default=dict, encoder=CustomJsonEncoder
)
- variants = models.ManyToManyField("product.ProductVariant", blank=True)
+ variants = models.ManyToManyField( # type: ignore[var-annotated]
+ "product.ProductVariant", blank=True, through="PromotionRule_Variants"
+ )
reward_value_type = models.CharField(
max_length=255, choices=RewardValueType.CHOICES, blank=True, null=True
)
@@ -429,6 +431,18 @@ class PromotionRule(models.Model):
return cursor.fetchall()
+class PromotionRule_Variants(models.Model):
+ id = models.BigAutoField(primary_key=True, editable=False, unique=True)
+ promotionrule = models.ForeignKey(
+ PromotionRule,
+ on_delete=models.CASCADE,
+ )
+ productvariant = models.ForeignKey(
+ "product.ProductVariant",
+ on_delete=models.CASCADE,
+ )
+
+
class PromotionRuleTranslation(Translation):
name = models.CharField(max_length=255, null=True, blank=True)
description = SanitizedJSONField(blank=True, null=True, sanitizer=clean_editor_js)