Re: permissions and groups data migration

1,491 views
Skip to first unread message

Andrew Godwin

unread,
Oct 14, 2014, 10:09:55 PM10/14/14
to django-d...@googlegroups.com
As the ticket suggests, you can call the function to create permissions yourself in the data migration, and then you can assign them as normal. There's no need to use fixtures (in fact, migrations are better without fixtures, as there's no easy way to load them).

Andrew

On Tue, Oct 14, 2014 at 6:56 PM, Michael <michael....@gmail.com> wrote:
Hi,

Since the permissions are not yet created when the migrations are run, it is not possible to have a data migration that creates a group and assigns permissions.

Based on this ticket: https://code.djangoproject.com/ticket/23422, I understand there will not be a fix. Do you recommend to keep using an initial fixture to create a group?

Thanks
Michael




--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/cce3b138-6524-417c-b05e-cd3c25742e3b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael

unread,
Oct 15, 2014, 8:43:03 AM10/15/14
to django-d...@googlegroups.com
I do not want to  create a permission, I want to assign to a group some of the default permissions django creates: add, change and delete (https://docs.djangoproject.com/en/dev/topics/auth/default/#default-permissions)

Obviously, the permissions are not created yet when I run my first python manage.py migrate since the following will raise a "Permission matching query does not exist."
Permission.objects.get(codename=codename)
Permission.objects.get_by_natural_key('add_my_model', 'my_app', 'my_model')

Do you recommend to manually create the default permissions I am interested about?

Michael

unread,
Oct 15, 2014, 8:52:23 AM10/15/14
to django-d...@googlegroups.com
Also, ContentType.objects.get_for_model does not work ('Manager' object has no attribute 'get_for_model').

Carl Meyer

unread,
Oct 15, 2014, 1:10:19 PM10/15/14
to django-d...@googlegroups.com
On 10/15/2014 06:52 AM, Michael wrote:
> Also, ContentType.objects.get_for_model does not work ('Manager' object
> has no attribute 'get_for_model').

Not sure, but it might work to instead call
`django.contrib.auth.management.create_permissions(...)` in the
migration? You'll need to get hold of the AppConfig for the app whose
permissions you want to create; you should be able to get this from the
`apps` argument to your data migration.

Carl

Adam Venturella

unread,
Dec 31, 2014, 12:01:11 AM12/31/14
to django-d...@googlegroups.com
Attempting this approach, calling :

create_permissions(my_app_config)

from within my migration fails to create the permissions. It looks like create_permissions checks for models_module on the provided app config. However, when my migration runs, my models_module is None. 

So is there a better time when models_module is available?  My understanding of models_module is that I have a models.py in my app, which I indeed do. I can call 

my_app_config.get_models()

and I get a list of all of my models, but my_app_config.models_module remains None

If I try this from the django shell:

from django.apps import apps
apps.get_app_config('my_app').models_module


The result here is not None, so it seems the issue is migration related.

Aymeric Augustin

unread,
Dec 31, 2014, 8:36:15 AM12/31/14
to django-d...@googlegroups.com
Hi Adam,

Migrations build a fake app registry containing fake app configs for each state of the models.

They implement a small subset of features of the real app configs.


That’s why my_app_config.models_module is None in your example.

-- 
Aymeric.



Luis-José Torres

unread,
Jan 15, 2015, 2:03:07 PM1/15/15
to django-d...@googlegroups.com
Hi,

This worked for me:

from django.apps.registry import apps as apps_alt
from django.contrib.auth.management import create_permissions

for app_config in apps_alt.get_app_configs():
    create_permissions
(app_config)

Andrew Grigorev

unread,
Mar 4, 2015, 2:15:22 PM3/4/15
to django-d...@googlegroups.com
Thank you, Luis-José Torres!

On the basis of your code I added the following to my migrations:

def fix_perms(*app_labels):
   
def wrapped(apps, schema_editor):
       
from django.apps.registry import apps
       
from django.contrib.contenttypes.management import update_contenttypes
       
from django.contrib.auth.management import create_permissions
       
for app in app_labels:
            app_conf
= apps.get_app_config(app)
            update_contenttypes
(app_conf, app_conf.get_models())
            create_permissions
(app_conf, interactive=False)
   
return wrapped


class Migration(migrations.Migration):
    operations
= [
        migrations
.RunPython(fix_perms("books")),
        migrations
.RunPython(add_some_group_permissions_on_books),
   
]



четверг, 15 января 2015 г., 22:03:07 UTC+3 пользователь Luis-José Torres написал:
Reply all
Reply to author
Forward
0 new messages