Improvements to django.forms.ModelForm - validates unique_together and UniqueConstraint
Project description
django-modelforms
This project came about because our reusable administration has to deal with
related models that have unique_together constraints (and modern UniqueConstraint)
that were not being properly validated by the default implementation.
It occurs because our forms don't show the related field, it's not something we want the user to be able to edit. This has the nasty side-effect of excluding the related field from validation checks, so uniqueness (with respect to the related field) is not tested.
The form therefore validates, and a database update is attempted - only to fail when integrity constraints are enforced by the backend.
Installation
pip install django-modelforms
Or with uv:
uv add django-modelforms
Usage
Simply use modelforms.forms.ModelForm instead of django.forms.ModelForm:
from modelforms.forms import ModelForm
class BookForm(ModelForm):
class Meta:
model = Book
fields = ['title', 'rrp']
This will properly validate both legacy unique_together constraints and modern
UniqueConstraint definitions in your model's Meta.constraints.
Supported Django and Python Versions
- Django 4.2, 5.2, 6.0
- Python 3.10, 3.11, 3.12, 3.13
Why?
Django bug #13091 was filed in March 2010 and still has not been fixed.
There has been discussion on the mailing lists and a number of patches proposed, but none have been accepted.
To stop encountering this in our projects we decided to write something generic that can easily be used without having to wait for an upstream fix.
Development
This project uses uv and tox for development:
# Install development dependencies
uv sync
# Run tests
uv run tox
# Run tests for a specific environment
uv run tox -e py312-django52
# Run linting
uv run tox -e lint
License
BSD-3-Clause
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_modelforms-1.0.1.tar.gz.
File metadata
- Download URL: django_modelforms-1.0.1.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f9ac32f41ff66ab3884d56f08fa12b2dae5cc2964ddd5a1f620a8cc35ed8c53
|
|
| MD5 |
6f33b11f2c72bd349e311c72a56be130
|
|
| BLAKE2b-256 |
0b8eaedd5b58eeef8d5ff78f30439dca05194278b426dfc1b9b19799b6427e92
|
Provenance
The following attestation bundles were made for django_modelforms-1.0.1.tar.gz:
Publisher:
publish.yml on goodtune/django-modelforms
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_modelforms-1.0.1.tar.gz -
Subject digest:
8f9ac32f41ff66ab3884d56f08fa12b2dae5cc2964ddd5a1f620a8cc35ed8c53 - Sigstore transparency entry: 924398261
- Sigstore integration time:
-
Permalink:
goodtune/django-modelforms@a0339f08d66c4f356d8b90b18b6e6f443bf62374 -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/goodtune
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a0339f08d66c4f356d8b90b18b6e6f443bf62374 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_modelforms-1.0.1-py3-none-any.whl.
File metadata
- Download URL: django_modelforms-1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20895c85a0d7ebc34009e43297825bde7031eb755dac12c1f244fd5587c5867f
|
|
| MD5 |
aae38cd3a1afa6551632d43541f9286f
|
|
| BLAKE2b-256 |
4218f8ba58ae2b33ed79bd12d96ca5ffa83820282a7e30549b92cff4016a2238
|
Provenance
The following attestation bundles were made for django_modelforms-1.0.1-py3-none-any.whl:
Publisher:
publish.yml on goodtune/django-modelforms
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_modelforms-1.0.1-py3-none-any.whl -
Subject digest:
20895c85a0d7ebc34009e43297825bde7031eb755dac12c1f244fd5587c5867f - Sigstore transparency entry: 924398263
- Sigstore integration time:
-
Permalink:
goodtune/django-modelforms@a0339f08d66c4f356d8b90b18b6e6f443bf62374 -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/goodtune
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a0339f08d66c4f356d8b90b18b6e6f443bf62374 -
Trigger Event:
release
-
Statement type: