Skip to main content

A Django package for implementing field-level permissions on model fields.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_field_permissions-1.1.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_field_permissions-1.1.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file django_field_permissions-1.1.0.tar.gz.

File metadata

File hashes

Hashes for django_field_permissions-1.1.0.tar.gz
Algorithm Hash digest
SHA256 8a251a35345c6098d9c31f8af362bbdc8cab73772ddd544e4648245928715ace
MD5 3c2792cc60e6afe6a299649b5f078c47
BLAKE2b-256 0af275762393bcee324dd846c66d7080a674f8b2fa09ba55f20e70be1c445fab

See more details on using hashes here.

File details

Details for the file django_field_permissions-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_field_permissions-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 014b1768edb45d209deebde4fbde1161a8eb6fafc76aa0f0cf02ef4a3c4de099
MD5 a8fba518e03fa13ae76cc2f3317c7d08
BLAKE2b-256 6b08150cfde80432f6578e28cdc927076b4ea848e9bd611dd316d6ee5514b108

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page