django: data migrate permissions

后端 未结 3 1771
自闭症患者
自闭症患者 2021-01-12 20:47

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.

3条回答
  •  一整个雨季
    2021-01-12 21:14

    Here is a quick and dirty way to ensure all permissions for all apps have been created:

    def add_all_permissions():
        from django.apps import apps
        from django.contrib.auth.management import create_permissions
    
        for app_config in apps.get_app_configs():
            app_config.models_module = True
            create_permissions(app_config, verbosity=0)
            app_config.models_module = None
    

提交回复
热议问题