Giving default permissions or a default group to new users

后端 未结 1 980
情书的邮戳
情书的邮戳 2021-02-13 12:31

Default behaviour seems to be new users have no permissions and no groups. However I\'d like not to have to manually grant every single new users basic permissions, and I\'d ass

相关标签:
1条回答
  • 2021-02-13 13:04

    You can subscribe to post_save signal on User model and put newly created user to desired group or add permissions.

    from django.contrib.auth.models import Group
    
    def add_to_default_group(sender, **kwargs):
        user = kwargs["instance"]
        if kwargs["created"]:
            group = Group.objects.get(name='groupname')
            user.groups.add(group)
    

    And on django 1.8+ put following code into your AppConfig.ready()

    from django.conf import settings
    
    post_save.connect(add_to_default_group, sender=settings.AUTH_USER_MODEL)
    
    0 讨论(0)
提交回复
热议问题