扩展django组和权限 [英] Extend django Groups and Permissions

查看:521
本文介绍了扩展django组和权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我过去3天一直在搜索Google和Stackoverflow,找不到任何类似的东西。我想扩展或使用django组和权限。

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)

现在 - 一切都很好。现在我想要的是扩展django传递的现有Auth.Group,所以我可以使用 request.user.has_perm ... 直接的方式。

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.

所以,让我们将团队模型更改为

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.

使用自定义保存方法我也可以添加,用户成员中列出的c $ c>将被添加到创建的,并获得授予团队/组的权限<

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.

现在我的问题是,名称字段唯一 - 所以我无法为每个项目添加一个 Admins -Team。

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!

推荐答案

你需要使组名独一无二。因此,您发现的一种方法是具有另一个名称字段,使其对于所有子组都是唯一的。

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.

另一种方法是使用现有名称字段并添加特殊字符你的。例如,组名为 admin#project1 member#project1 等(注意:我不确定'# '可以在组名中使用,您可以选择允许的任何特殊字符。)

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).

所以每当你创建团队团队中使用后缀#< project_name> 更新名称字段 save )方法。

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

要更清楚地显示,可以添加 __ unicode __()方法只返回组名的第一部分。示例:

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

这篇关于扩展django组和权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆