#23422: Cannot add Permission to Group in data migration
----------------------------+--------------------------------------
Reporter: tjwalch | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 1.7
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------
Comment (by jwhitlock):
Here's an example of calling `create_permissions` directly from a data
migration, as suggested by carljm:
{{{
def make_permissions(apps, schema_editor, with_create_permissions=True):
Group = apps.get_model("auth", "Group")
Permission = apps.get_model("auth", "Permission")
try:
perm = Permission.objects.get(
codename='add_mymodel', content_type__app_label='my_app')
except Permission.DoesNotExist:
if with_create_permissions:
# Manually run create_permissions
from django.contrib.auth.management import create_permissions
assert not getattr(apps, 'models_module', None)
apps.models_module = True
create_permissions(apps)
apps.models_module = None
return make_permissions(
apps, schema_editor, with_create_permissions=False)
else:
raise
my_new_group = Group.objects.create(name="test_group")
my_new_group.permissions.add(perm)
}}}
Feels a little hacky, but it solved this `wontfix` for me . I'd prefer a
migration configuration that says "stop migrating, emit `post_migrate`
signals, and restart migrations."
--
Ticket URL: <https://code.djangoproject.com/ticket/23422#comment:6>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.94576d82e90dd87aa004f7e8f21b913b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.