From 0862c0f0bc655616436d02f04e5a54d431652e46 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Tue, 6 Apr 2021 18:10:22 +0200 Subject: [PATCH] add migration to automatically assign group to existing superusers --- .../migrations/0118_auto_20210406_1805.py | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 cookbook/migrations/0118_auto_20210406_1805.py diff --git a/cookbook/migrations/0118_auto_20210406_1805.py b/cookbook/migrations/0118_auto_20210406_1805.py new file mode 100644 index 00000000..53a7bcd0 --- /dev/null +++ b/cookbook/migrations/0118_auto_20210406_1805.py @@ -0,0 +1,25 @@ +# Generated by Django 3.1.7 on 2021-04-06 16:05 + +from django.db import migrations +from django_scopes import scopes_disabled + + +def migrate_no_group_superusers(apps, schema_editor): + with scopes_disabled(): + User = apps.get_model('auth', 'User') + Groups = apps.get_model('auth', 'Group') + + for u in User.objects.filter(is_superuser=True).all(): + if u.groups.count() == 0: + u.groups.add(Groups.objects.get(name='admin')) + + +class Migration(migrations.Migration): + + dependencies = [ + ('cookbook', '0117_space_max_recipes'), + ] + + operations = [ + migrations.RunPython(migrate_no_group_superusers), + ]