Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django: data migrate permissions

I have a bunch of new permissions which I need to migrate. I tried doing it through data migration but complains about ContentType not being available.

Doing quick research I found out that ContentType table is populated after all the migrations applied.

I even tried using update_all_contenttypes() from from django.contrib.contenttypes.management import update_all_contenttypes which causes migration to load data which is not consistent to the fixture.

What is the best way to migrate permission data in Django?

like image 829
learner010 Avatar asked Mar 27 '15 09:03

learner010


1 Answers

There are 2 ways to solve this:

1) The ugly way:

Run manage.py migrate auth before your wanted migration

2) Recommended way:

from django.contrib.auth.management import create_permissions

def add_permissions(apps, schema_editor):
    apps.models_module = True

    create_permissions(apps, verbosity=0)
    apps.models_module = None

    # rest of code here....
like image 70
elad silver Avatar answered Sep 19 '22 18:09

elad silver