Skip to main content

django-field-permissions

Django's built-in permission system works at the model level — a user can either access a model or they can't. There isn't a built-in way to say "this user can see the email field but not edit it" without a custom implementation. django-field-permissions fills that gap by adding field-level read and edit permissions that can be assigned to individual users or groups.

PyPI version License: MIT


Overview

django-field-permissions introduces a FieldPermission model that maps model fields to an access level (read or edit) and a set of users and/or groups. A middleware resolves the current user's permissions on every request and attaches them to request.field_perms. A template tag and utility function let you check those permissions anywhere in your templates or views.

Key features:

  • Per-field read and edit access levels
  • Assign permissions to individual users, groups, or both
  • Check a user's field permissions in the template or backend
  • Superusers automatically pass all permission checks, following Django convention
  • Middleware-driven — resolved permissions are available on every request via request.field_perms
  • Built-in caching with automatic invalidation via Django signals
  • Django admin integration for managing permissions through the UI

Installation

pip install django-field-permissions

Quick Start

1. Add to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'field_permissions',
]

2. Add the middleware:

MIDDLEWARE = [
    ...
    'field_permissions.middleware.FieldPermissionMiddleware',
]

Place this after AuthenticationMiddleware.

3. Declare which models get field permissions:

# settings.py
FIELD_PERMISSIONS_ALLOWED_MODELS = [
    'myapp.MyModel',
    'otherapp.AnotherModel',
]

4. Run migrations and sync permissions:

python manage.py migrate
python manage.py sync_field_permissions

This creates one read record and one edit record in the database for every field on every model listed in FIELD_PERMISSIONS_ALLOWED_MODELS.

5. Assign permissions in the Django admin:

Optional — wire up admin mixins to manage permissions from the User or Group admin pages:

from django.contrib import admin
from django.contrib.auth.models import User, Group
from django.contrib.auth.admin import UserAdmin, GroupAdmin
from field_permissions.admin import FieldPermissionUserAdminMixin, FieldPermissionGroupAdminMixin

class MyUserAdmin(FieldPermissionUserAdminMixin, UserAdmin):
    pass

class MyGroupAdmin(FieldPermissionGroupAdminMixin, GroupAdmin):
    pass

admin.site.unregister(User)
admin.site.unregister(Group)
admin.site.register(User, MyUserAdmin)
admin.site.register(Group, MyGroupAdmin)

A field permissions FilteredSelectMultiple widget is added to the User and Group edit and create pages in Django admin.

Otherwise permission records can be created via SQL / Django Shell.

6. Check permissions in templates:

{% load field_permissions %}

# Format: request|has_field_perm:"model_name,field_name,access_level
# Returns True/False

{% if request|has_field_perm:"mymodel,email,read" %}
    {{ user.email }}
{% endif %}

7. Check permissions in views:

from field_permissions.permissions import has_field_perm

def my_view(request):
    # Format: has_field_perm(request, 'model_name', 'field_name', 'access_level')
    # Returns True/False

    if has_field_perm(request, 'mymodel', 'email', 'edit'):
        # allow edit
        pass

Configuration

All settings are optional, add any of the following to your settings.py:

Setting Default Description
FIELD_PERMISSIONS_ALLOWED_MODELS [] Models to create field permissions for. Format: ["appname.ModelName"]
FIELD_PERMISSIONS_ENABLE True Enable or disable the middleware globally
FIELD_PERMISSIONS_USE_CACHE True Cache resolved permissions per user
FIELD_PERMISSIONS_CACHE_TIMEOUT 3600 Cache TTL in seconds (default: 1 hour)

Caches are automatically invalidated when any FieldPermission record or its user/group assignments change.


License

MIT — see LICENSE for details.

Release files for django-field-permissions 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-field-permissions 1.1.0
File Size Uploaded
django_field_permissions-1.1.0.tar.gz 8.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-field-permissions 1.1.0
File Interpreter ABI Platform
django_field_permissions-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.3 kB

Release files / django_field_permissions-1.1.0.tar.gz

Download URL django_field_permissions-1.1.0.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8a251a35345c6098d9c31f8af362bbdc8cab73772ddd544e4648245928715ace
BLAKE2b-256 checksum
How to use checksums
0af275762393bcee324dd846c66d7080a674f8b2fa09ba55f20e70be1c445fab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.2

Release files / django_field_permissions-1.1.0-py3-none-any.whl

Download URL django_field_permissions-1.1.0-py3-none-any.whl
Size 11.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
014b1768edb45d209deebde4fbde1161a8eb6fafc76aa0f0cf02ef4a3c4de099
BLAKE2b-256 checksum
How to use checksums
6b08150cfde80432f6578e28cdc927076b4ea848e9bd611dd316d6ee5514b108
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.2

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page