views:

330

answers:

2

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

A: 

Here is a basic example.

See: http://tiemonster.info/116

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.

Todd Moses
+2  A: 

I wasn't able to find anything, so I figured it out and wrote up a quick overview myself:

http://parand.com/say/index.php/2010/02/19/django-using-the-permission-system/

Parand