When to programmatically create Django user permissions?

The permission / authentication documentation for Django 1.4 provides the following piece of software for creating custom permissions:

Edit: (I would like to use this for permissions that are not necessarily associated with a particular model class, but more general permissions that span multiple types.)

from django.contrib.auth.models import Group, Permission from django.contrib.contenttypes.models import ContentType content_type = ContentType.objects.get(app_label='myapp', model='BlogPost') permission = Permission.objects.create(codename='can_publish', name='Can Publish Posts', content_type=content_type) 

A source

My question is where this code should be placed. Obviously, they should be created only once, but I do not want to do this in the shell. It seems that this should be stored in a file somewhere. (For documentation sake.)

+7
source share
1 answer

It is usually sufficient to simply add the necessary permissions to the corresponding model class using the permissions meta attribute.

This is from the official documentation:

 class Task(models.Model): ... class Meta: permissions = ( ("view_task", "Can see available tasks"), ("change_task_status", "Can change the status of tasks"), ("close_task", "Can remove a task by setting its status as closed"), ) 
+2
source

All Articles