#29843: Create permissions using migration operations rather than using the
post_migrate signal
-------------------------------------+-------------------------------------
Reporter: Petter Strandmark | Owner: Arthur
Type: | Rio
Cleanup/optimization | Status: assigned
Component: contrib.auth | Version: dev
Severity: Normal | Resolution:
Keywords: contenttypes | Triage Stage: Accepted
permissions post_migrate |
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Javier Buzzi):
Am I missing something thing here?
I've modified `apply_migration` only, kept `django<2.2` as defined in the
example `Pipfile` + `python 3.8` -- and this works exactly as expected. As
an added bonus, I tried `django==5.2` + `python 3.12`, and it also worked.
{{{
def apply_migration(apps, schema_editor):
Group = apps.get_model('auth', 'Group')
Permission = apps.get_model('auth', 'Permission')
ContentType = apps.get_model('contenttypes', 'ContentType')
ct, _ = ContentType.objects.get_or_create(app_label='auth',
model='user')
for group_name, perm_list in GROUPS.items():
group, _ = Group.objects.get_or_create(name=group_name)
# Assign permissions to groups.
for permission_code in perm_list:
permission, _ =
Permission.objects.get_or_create(codename=permission_code,
content_type=ct)
group.permissions.add(permission)
}}}
Can this be closed?
----
Here is the diff:
{{{
def apply_migration(apps, schema_editor):
+ Group = apps.get_model('auth', 'Group')
+ Permission = apps.get_model('auth', 'Permission')
+ ContentType = apps.get_model('contenttypes', 'ContentType')
+
+ ct, _ = ContentType.objects.get_or_create(app_label='auth',
model='user')
for group_name, perm_list in GROUPS.items():
- group, created =
Group.objects.get_or_create(name=group_name)
+ group, _ = Group.objects.get_or_create(name=group_name)
# Assign permissions to groups.
for permission_code in perm_list:
- permission =
Permission.objects.get(codename=permission_code)
+ permission, _ =
Permission.objects.get_or_create(codename=permission_code,
content_type=ct)
group.permissions.add(permission)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/29843#comment:18>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/0107019611046c72-eb844d47-de4b-4501-a3d1-5bfdc7872b3c-000000%40eu-central-1.amazonses.com.