Programmatically create Django security groups

Django authentication has security roles and CRUD permissions baked in from the get-go, but there’s a glaring omission: those roles, or Groups, are expected to be loaded by some competent administrator post-installation.  Groups are an excellent method of assigning access control to broad roles, but they don’t seem to be a first-class concept in Django.

It seems that you can kind-of save these values in by doing an export and creating a fixture, which will automatically re-load at install time, but that’s not terribly explicit – not compared to code. And I’m not even sure if it will work.  So here’s my solution to programmatically creating Django Groups., which is created in the same directory as your and is automatically run during python syncdb:

from django.db.models import signals
from django.contrib.auth.models import Group, Permission
import models 

myappname_group_permissions = {
  "Cinema Manager": [
    "delete_ticket",         # for sales reversals
    "add_creditcard_charge", # for sales reversals
  "Ticket Seller": [
  "Cleaner": [ # cleaners need to record their work

def create_user_groups(app, created_models, verbosity, **kwargs):
  if verbosity>0:
    print "Initialising data post_syncdb"
  for group in volunteer_group_permissions:
    role, created = Group.objects.get_or_create(name=group)
    if verbosity>1 and created:
      print 'Creating group', group
    for perm in myappname_group_permissions[group]: 
      if verbosity>1:
        print 'Permitting', group, 'to', perm

  sender=models, # only run once the models are created
  dispatch_uid='myappname.models.create_user_groups' # This only needs to universally unique; you could also mash the keyboard

And that’s it. Naturally, if the appropriate action_model permissions don’t exist there’s going to be trouble.  The code says: After syncdb is run on the models, call create_user_groups.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

One thought on “Programmatically create Django security groups

  1. Kibui Samuel

    Hello, great article.I love it.I have an application that will have four groups and each group will have to act on an application model (Applications).I wanted where each group of users will have to fill a certain field in the model to complete the entire processes.Which is the best way to handle the user Groups and Permissions .Am getting confused here. Am using django 1.7

Leave a Reply

Your email address will not be published.