0

I want to create a single class, UserGroup, that will contain Users and Groups. In the end I want to be able to give permission to some task based on whether the given User is in the Users M2M or is in the Groups M2M. I understand there are permissions in the Groups but I want to be able to give Permission to a single user (possibly) and I want a consistent interface. The class definition is (I have actually defined Member as User):

class UserGroup( models.Model ):

    users = models.ManyToManyField( Member )
    groups = models.ManyToManyField( Group )

And then in my views.py I think I will have something like:

def is_in( member ):

    ## Check to see if they are in the User m2m
    if members.objects.filter(pk=member.id).exists():
        return True

    ## Check to see if they are in the Group m2m
    if <something>:
        return True

    ## Otherwise return false.
    return False

I think this makes sense. What I am not sure of is an efficient method of checking to see if the member is in one of the groups in the Groups M2M.

(And if anyone wants to give me constructive comments on the whole class I would be happy to takes those too).

brechmos
  • 1,278
  • 1
  • 11
  • 22

1 Answers1

1

Django's admin interface and base User model already allows for this. Users can be added to groups and both groups/users can be given permissions. If you have extra needs beyond what is built-in you can start to override User or Group model methods.

In Django, how do I check if a user is in a certain group?

User groups and permissions

When viewing the User object in your admin, see the User Permissions sections. These are settings you can set per user that override the permissions set for the user by their group memberships.

Community
  • 1
  • 1
Ian Price
  • 7,416
  • 2
  • 23
  • 34
  • Yes, you are right. I had not looked at it hard enough. I do need to see about an interface to change the views of the permissions as i want to loop over each permission and show the people/groups. But that shouldn't be too difficult. – brechmos Sep 29 '14 at 14:31