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

后端 未结 3 967
死守一世寂寞
死守一世寂寞 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

    One solution is call the update_permissions command before try to append a permission

    from django.core.management import call_command
    
    def update_permissions(schema, group):
        call_command('update_permissions')
    
    
    operations = [
            migrations.RunPython(update_permissions, reverse_code=migrations.RunPython.noop),
            migrations.RunPython(create_group),
        ]
    

    And as was commented don't import Group and Permission models use:

    Group = apps.get_model("auth","Group")
    Permission = apps.get_model("auth","Permission")
    

提交回复
热议问题