Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add migration to fix menu on older instances
Pacu2d23465761ec3
diff --git a/saleor/menu/migrations/0007_auto_20180807_0547.py b/saleor/menu/migrations/0007_auto_20180807_0547.pynew file mode 100644index 0000000000..ab460144d7--- /dev/null+++ b/saleor/menu/migrations/0007_auto_20180807_0547.py@@ -0,0 +1,52 @@+# Generated by Django 2.0.3 on 2018-08-07 10:47+import json+from django.db import migrations+from saleor.dashboard.menu.utils import get_menu_item_as_dict+import jsonfield.fields+++def get_menu_as_json(menu):+ """Builds Tree-like structure from top menu items,+ its children and its grandchildren.+ """+ top_items = menu.items.filter(parent=None)+ menu_data = []+ for item in top_items:+ top_item_data = get_menu_item_as_dict(item)+ top_item_data['child_items'] = []+ children = item.children.all()+ for child in children:+ child_data = get_menu_item_as_dict(child)+ grand_children = child.children.all()+ grand_children_data = [+ get_menu_item_as_dict(grand_child)+ for grand_child in grand_children]+ child_data['child_items'] = grand_children_data+ top_item_data['child_items'].append(child_data)+ menu_data.append(top_item_data)+ return json.dumps(menu_data)+++def update_menus(apps, schema_editor):+ Menu = apps.get_model('menu', 'Menu')+ menus = Menu.objects.all()+ for menu in menus:+ menu.json_content = get_menu_as_json(menu)+ menu.save(update_fields=['json_content'])+++class Migration(migrations.Migration):++ dependencies = [+ ('menu', '0006_auto_20180803_0528'),+ ]++ operations = [+ migrations.AlterField(+ model_name='menu',+ name='json_content',+ field=jsonfield.fields.JSONField(blank=True, default={}),+ ),+ migrations.RunPython(+ update_menus, migrations.RunPython.noop),+ ]diff --git a/saleor/menu/models.py b/saleor/menu/models.pyindex d3d32fbfc2..9b1e974f25 100644--- a/saleor/menu/models.py+++ b/saleor/menu/models.py@@ -12,7 +12,7 @@ from ..product.models import Category, Collectionclass Menu(models.Model):name = models.CharField(max_length=128)- json_content = JSONField(blank=True, default='')+ json_content = JSONField(blank=True, default={})class Meta:permissions = ((