Skip to main content

Improvements to django.forms.ModelForm - validates unique_together and UniqueConstraint

Project description

django-modelforms

CI

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

django_modelforms-1.0.1.tar.gz (11.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_modelforms-1.0.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

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

Hashes for django_modelforms-1.0.1.tar.gz
Algorithm Hash digest
SHA256 8f9ac32f41ff66ab3884d56f08fa12b2dae5cc2964ddd5a1f620a8cc35ed8c53
MD5 6f33b11f2c72bd349e311c72a56be130
BLAKE2b-256 0b8eaedd5b58eeef8d5ff78f30439dca05194278b426dfc1b9b19799b6427e92

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_modelforms-1.0.1.tar.gz:

Publisher: publish.yml on goodtune/django-modelforms

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_modelforms-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_modelforms-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 20895c85a0d7ebc34009e43297825bde7031eb755dac12c1f244fd5587c5867f
MD5 aae38cd3a1afa6551632d43541f9286f
BLAKE2b-256 4218f8ba58ae2b33ed79bd12d96ca5ffa83820282a7e30549b92cff4016a2238

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_modelforms-1.0.1-py3-none-any.whl:

Publisher: publish.yml on goodtune/django-modelforms

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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