Programmatically creating a group : Can't access permissions from migration

后端 未结 3 940
死守一世寂寞
死守一世寂寞 2021-01-20 04:06

After seeing this post, I tried to create my own group at project setup with this migration :

from django.db import migrations
from django.contrib.auth.model         


        
3条回答
  •  鱼传尺愫
    2021-01-20 04:26

    From this Django ticket, here's what worked for me in Django 3.0.4 and apparently will work in >=1.9:

    from django.core.management.sql import emit_post_migrate_signal
    
    def create_group(apps, schema_editor):
        # Ensure permissions and content types have been created.
        db_alias = schema_editor.connection.alias
        emit_post_migrate_signal(2, False, db_alias)
        # Now the content types and permissions should exist
    
        Permission = apps.get_model('auth', 'Permission')
        ...
    

提交回复
热议问题