开发者

Django: Group based permissions example / docs?

C开发者_运维技巧an a kind soul point me to some good documentation or code samples on setting up group based permissions with Django? My requirements are fairly simple - I just need to enable/disable functionality based on what groups a user belongs to.


Here is a basic example.

See: http://www.thinkjson.com/group-permissions-in-django/

First of all, say you have a model called Report.

class Report(models.Model):
    name = models.CharField(max_length=100)
    contents = models.TextField(blank=True)
    authorized_groups = models.ManyToManyField('ReportGroup', null=True, blank=True, related_name='report_groups')    
    def __str__(self):
        return self.name

You can create an intermediary model to the User model to handle group permissions:

class ReportGroup(models.Model):
    name = models.CharField(max_length=100)
    authorized_users = models.ManyToManyField(User, null=True, blank=True, related_name='report_users')
    def __str__(self):
        return self.name

Now, when you are editing a report in the Django admin, you can assign group permissions to a report. These groups can be administered as Report Groups in the Django admin, letting you select in one shot who belongs to a group.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜