Create Custom Permissions

A snippet showing how to add custom permissions for Django models.


# models.py

from django.db import models

class Article(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()

    class Meta:
        permissions = [
            ("can_publish", "Can publish articles"),
            ("can_archive", "Can archive articles"),
        ]
  

# Django Admin

1. Go to /admin/auth/group/
2. Select or create a group
3. Assign your custom permissions like:
   - Can publish articles
   - Can archive articles
  
Explanation:
  • You can define custom permissions inside the model's Meta class using the permissions attribute.
  • Each permission is a tuple with a codename and a human-readable label.
  • These permissions appear in the Django admin and can be assigned to users or groups.
  • Use user.has_perm('yourapp.can_publish') to check permissions in views.
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.