Registry / web-framework / django-permissionedforms

django-permissionedforms

JSON →
library0.1pypypi✓ verified 25d ago

Django extension for creating forms that vary according to user permissions. It allows you to define forms where certain fields are shown or omitted based on the user's permissions. This library is currently at version 0.1 and has an active development cadence.

pip install django-permissionedforms
INSTALL
IMPORT
SIG · DJANGO-PERMISSIONE
D
django-permissionedforms
web-frameworkpythonv0.1
Install
3.5s avg
Import
679ms
Disk
66MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.720s · 66.3MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.5s · import 0.638s · 67MB
66MB installed
● package 66MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

PermissionedForm
from permissionedforms import PermissionedForm
PermissionedModelForm
from permissionedforms import PermissionedModelForm

To create a permissioned form, subclass either `PermissionedForm` or `PermissionedModelForm`. Define an inner `Meta` class and set `field_permissions` as a dictionary mapping field names to Django permission codenames (e.g., 'app_label.codename'). Instantiate the form by passing the `for_user` keyword argument with a Django user object. Fields for which the user lacks permission will be omitted from the form.

from django import forms from permissionedforms import PermissionedForm, PermissionedModelForm # Example 1: Basic Form class PersonForm(PermissionedForm): first_name = forms.CharField(max_length=100) last_name = forms.CharField(max_length=100) class Meta: field_permissions = { 'last_name': 'myapp.change_last_name_field' } # Example 2: Model Form # from django.db import models # class Country(models.Model): # name = models.CharField(max_length=100) # description = models.TextField(blank=True) # class Meta: # app_label = 'myapp' # permissions = [('change_country_description', 'Can change country description')] # class CountryForm(PermissionedModelForm): # class Meta: # model = Country # fields = ['name', 'description'] # field_permissions = { # 'description': 'myapp.change_country_description' # } # Usage in a view (assuming 'request' is an HttpRequest object) # user = request.user # A Django User object # form = PersonForm(for_user=user) # if form.is_valid(): # pass # model_instance = Country.objects.first() # or create one # model_form = CountryForm(instance=model_instance, for_user=user)
Debug
Known issues
breakingAs this library is at version 0.1, the API is still considered unstable. Future minor or major versions may introduce breaking changes without extensive deprecation cycles. Users should pin exact versions and review release notes carefully for upgrades.
fix
Monitor GitHub releases and changelogs. Pin exact dependency versions (e.g., `django-permissionedforms==0.1`).
affects: 0.1.x
gotchaIncorrect configuration of Django's native permission system (e.g., missing permissions, misnamed codenames, or improper `AUTHENTICATION_BACKENDS`) can lead to fields being unexpectedly hidden or shown. Ensure your permissions are correctly defined and applied to users/groups.
fix
Verify that custom permissions are defined in an app's `models.py` or through migrations. Ensure the user has the expected permissions via `user.has_perm('app_label.codename')`. Check `settings.py` for `AUTHENTICATION_BACKENDS` if custom permission logic is involved.
affects: 0.1.x
gotchaWhen integrating `PermissionedForm` or `PermissionedModelForm` with another custom base form class that defines its own metaclass, direct multiple inheritance might fail. A custom metaclass inheriting from both `PermissionedFormMetaclass` and the other form's metaclass may be required.
fix
If `FancyForm` has a custom metaclass, define a new metaclass like `class FancyPermissionedFormMetaclass(PermissionedFormMetaclass, FancyFormMetaclass): pass` and use it in your combined form: `class FancyPermissionedForm(PermissionedForm, FancyForm, metaclass=FancyPermissionedFormMetaclass): pass`.
affects: 0.1.x
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'permissionedforms'
The 'django-permissionedforms' package is either not installed in your environment or there is a typo in the import statement.
fix
Install the package using pip: `pip install django-permissionedforms`. Ensure your import statement is `from permissionedforms import PermissionedForm` or `from permissionedforms import PermissionedModelForm`.
AttributeError: 'PermissionedForm' object has no attribute 'field_name'
You are attempting to access form field data directly as an attribute of the form instance (e.g., `form.field_name`) before or after validation. In Django forms, submitted and validated data is accessed via the `cleaned_data` dictionary.
fix
After validating the form with `form.is_valid()`, access the field's data using `form.cleaned_data['field_name']` or `form.cleaned_data.get('field_name')`.
AttributeError: 'NoneType' object has no attribute 'has_perm'
This error occurs when the `PermissionedForm` or `PermissionedModelForm` attempts to check user permissions but the `for_user` argument was not provided during form instantiation, resulting in an attempt to call `has_perm` on a `None` object.
fix
When instantiating your form, ensure you pass the `for_user` keyword argument with the current request's user object: `form = MyPermissionedForm(for_user=request.user)`.
Upgrade
Version history
0.1latest on PyPI · released Feb 28, 2022
Audit
Dependencies
DjangorequiredCore framework dependency, forms are built on Django's forms framework. Requires Django 3.7+ (implied by Python 3.7+ requirement and Django 5.2+ seen in distribution metadata).
Agent activity
23 hits · last 30 days
node
20
OpenAI (training)
1
Resources
django-permissionedforms — pip install django-permissionedforms · libregistry