Skip to content

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

Improve adding category migration

Anna Siwikc26169a7ba54
diff --git a/saleor/product/migrations/0046_product_category.py b/saleor/product/migrations/0046_product_category.py
index 6081bdd17d..25968f9ef1 100644
--- a/saleor/product/migrations/0046_product_category.py
+++ b/saleor/product/migrations/0046_product_category.py
@@ -8,13 +8,18 @@ import django.db.models.deletion
def assign_category_to_products(apps, schema_editor):
Product = apps.get_model('product', 'Product')
- Category = apps.get_model('product', 'Category')
- default_category = Category.objects.first()
for product in Product.objects.all():
- product.category = product.categories.first() or default_category
+ product.category = product.categories.first()
product.save()
+def assign_categories_to_products(apps, schema_editor):
+ Product = apps.get_model('product', 'Product')
+ for product in Product.objects.all():
+ if product.category:
+ product.categories.add(product.category)
+
+
class Migration(migrations.Migration):
dependencies = [
@@ -27,5 +32,5 @@ class Migration(migrations.Migration):
name='category',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='product.Category'),
),
- migrations.RunPython(assign_category_to_products, migrations.RunPython.noop),
+ migrations.RunPython(assign_category_to_products, assign_categories_to_products),
]