Extend django Groups and Permissions

2019-02-02 18:11发布

问题:

I've been searching Google and Stackoverflow for the last 3 days now and couldn't find anything similar. I'd like to extend or use the django groups and permissions.

Let's say we got some Projects with different Teams and Users.

class Project(models.Model):
    owner = models.ForeignKey(User)
    name = models.CharField(max_length=100)

class Team(models.Model):
    project = models.ForeignKey(Project)
    members = models.ManyToManyField(User)
    name = models.CharField(max_length=100)
    permission = models.ManyToManyField(Permission)

Now - everything's fine. Now what I want is to extend the existing Auth.Group delivered by django so I could use request.user.has_perm... the direct way.

So, let's change the Team-Model into

class Team(Group):
    project = models.ForeignKey(Project)
    members = models.ManyToManyField(User)

This extends the existing Group. The name and permissions field comes direct from the Group-Model. If I create a new Team, there will be a Group added, too. That's great.

With a custom save method I could also add, that the user listed in members, will be added to the created Group and gets the permissions granted to the Team/Group.

Now my problem is, the name field of the Group is unique - so I couldn't add an Admins-Team for each Project.

I thought about adding another name-field for the frontend and generate a unique string for the name field of the Group-Model. But hmmm....is there a better solution for multiple custom Groups? Maybe one got an idea? Thank you in advance!

回答1:

You need to make group names unique. So one way that you found out is having another name field, making it unique for all subgroups.

Another way would be to make use of existing name field and add special chars of yours. For example group name with admin#project1, members#project1 etc. (Note: I'm not sure '#' is allowed in group name, you can choose any special character that is allowed).

So whenever you create Team you update the name field with suffix #<project_name> in Team models save() method.

To display it more sanely, you can add __unicode__() method to return only first part of the group name. Example:

class Team(Group):
    ...
    def __unicode__(self):
        try:
            return self.name.split('#')[0]
        except:
            #something wrong!
            return self.name