From fa556c9a7ff1667180dc6501b2ea84ddbb0a3bb0 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Sat, 26 Dec 2020 17:20:17 +0100 Subject: [PATCH] added migration to fix emtpy units set all units to none for all recipes containing empty named units and delete them --- .../migrations/0091_auto_20201226_1551.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 cookbook/migrations/0091_auto_20201226_1551.py diff --git a/cookbook/migrations/0091_auto_20201226_1551.py b/cookbook/migrations/0091_auto_20201226_1551.py new file mode 100644 index 000000000..d952c4ea8 --- /dev/null +++ b/cookbook/migrations/0091_auto_20201226_1551.py @@ -0,0 +1,27 @@ +# Generated by Django 3.1.4 on 2020-12-26 14:51 + +from django.db import migrations + + +def migrate_empty_units(apps, schema_editor): + Unit = apps.get_model('cookbook', 'Unit') + Step = apps.get_model('cookbook', 'Step') + + empty_units = Unit.objects.filter(name='').all() + for x in empty_units: + for s in Step.objects.all(): + for i in s.ingredients.all(): + if i.unit == x: + i.unit = None + i.save() + x.delete() + + +class Migration(migrations.Migration): + dependencies = [ + ('cookbook', '0090_auto_20201214_1359'), + ] + + operations = [ + migrations.RunPython(migrate_empty_units), + ]