I read in Django documentation that when I add an User in a group, it will automatically have the permissions granted to that group. So, in my application I created a group called "admin" via admin interface and I gave the permission I needed to it. Now if I create a new User and add it to the group "owners", it doesn't have the permissions of the group.
Then I tried to experiment it in django shell and it ran well
The permissions of the group really assigned to the user_permissions
That's my code in django user model
class UserAccount(AbstractUser):
def __str__(self): # __unicode__ for Python 2
return str(self.username)
def __unicode__(self):
return str(self.username)
def save(self, *args, **kwargs):
super(UserAccount, self).save(*args, **kwargs)
for group in set(self.groups.all()):
self.user_permissions = group.permissions.all()
Another Try
class UserAccount(AbstractUser):
def __str__(self): # __unicode__ for Python 2
return str(self.username)
def __unicode__(self):
return str(self.username)
def save(self, *args, **kwargs):
super(UserAccount, self).save(*args, **kwargs)
for group in set(self.groups.all()):
for perm in group.permissions.all():
self.user_permissions.add(perm)
Both not working when I save the user from the admin panel interface but both working well at the django shell
I tried to debug ipdb
and the code executed but permissions not assigned
Then I try to add permissions in reverse way from group
from django.contrib.auth.models import Group as BaseGroup
class Group(BaseGroup):
class Meta:
proxy = True
def save(self):
super(Group, self).save()
for user in self.user_set.all():
user.user_permissions = self.permissions.all()
I don't know if what I did is right or not and if it is right why not work properly.
Update:
No need to assign the permissions as the user has the permissions of the group automatically