Adding new custom permissions in Django

前端 未结 5 1486
攒了一身酷
攒了一身酷 2021-01-29 22:46

I am using custom permissions in my Django models like this:

class T21Turma(models.Model):
    class Meta:
        permissions = ((\"can_view_boletim\", \"Can vi         


        
5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-29 23:35

    You can connect to the post_migrate signal in order to update the permissions after migration. I use the following code, slightly modified from Dev with Passion and originally from django-extensions.

    # Add to your project-level __init__.py
    
    from south.signals import post_migrate
    
    def update_permissions_after_migration(app,**kwargs):
        """
        Update app permission just after every migration.
        This is based on app django_extensions update_permissions management command.
        """
        from django.conf import settings
        from django.db.models import get_app, get_models
        from django.contrib.auth.management import create_permissions
    
        create_permissions(get_app(app), get_models(), 2 if settings.DEBUG else 0)
    
    post_migrate.connect(update_permissions_after_migration)
    

提交回复
热议问题