A Django mixin for easily including reverse relationships in your changeforms in the admin
Project description
django-admin-reversefields
Manage reverse ForeignKey / OneToOne relationships from the parent change form in Django admin.
django-admin-reversefields adds parent-side selector fields (single or multi-select) that stay in sync with the child rows on save — without hand-rolling custom forms, querysets, or save logic.
Quickstart · Recipes · Caveats
Why
Django admin gives you inlines for reverse relations. They’re great for creating or editing related rows, but they don’t give you a simple selector to attach existing child objects to a parent from the parent form.
When you implement this yourself, you usually repeat the same plumbing:
- form field + widget wiring
- queryset filtering
- initial value population
- unbind / bind save logic
- transaction handling
This package turns that pattern into a reusable mixin + config.
What it does
You have a parent model (Organization / Company / Tenant) and a child model (Site / Department / Project) where the child has a ForeignKey to the parent.
This package adds virtual fields to the parent admin form so you can select which existing child rows belong to the parent. On save, it synchronizes the child rows’ foreign keys to match the selection.
Supports:
- single-select or multi-select
- choice filtering (example: “unassigned or already assigned to this parent”)
- transactional synchronization on save
Install
pip install django-admin-reversefields
Supported versions:
- Django: 4.2, 5.0, 5.1, 5.2
- Python: 3.10–3.13
Quickstart
Example: assign Site rows to an Organization from the Organization admin page.
from django.contrib import admin
from django_admin_reversefields.mixins import ReverseRelationAdminMixin, ReverseRelationConfig
@admin.register(Organization)
class OrganizationAdmin(ReverseRelationAdminMixin, admin.ModelAdmin):
reverse_relations = {
"sites": ReverseRelationConfig(
model=Site,
fk_field="organization",
multiple=True,
limit_choices_to=unbound_or_current, # optional
),
}
fieldsets = (
("Details", {"fields": ("name",)}),
("Sites", {"fields": ("sites",)}),
)
Include each virtual field name in fieldsets (or fields). When you save the parent, the mixin updates the child rows’ foreign keys to match the current selection.
Development
This project uses uv for local tooling.
Setup:
uv sync
Common commands:
uv run ruff check .
uv run django-admin test
uv run sphinx-build -b html docs docs/_build/html -W
Run the demo/test app:
export DJANGO_DB_NAME=db.sqlite3
uv run python manage.py migrate
uv run python manage.py seed
uv run python manage.py runserver
Log in at /admin/ with admin / admin and edit a company to see the reverse fields.
Release
uv build
twine upload dist/*
License
See LICENSE
Project details
Release history Release notifications | RSS feed
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_admin_reversefields-0.2.0.tar.gz.
File metadata
- Download URL: django_admin_reversefields-0.2.0.tar.gz
- Upload date:
- Size: 15.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a5fc635fd1e0099fe48215670556b05944bd0305ed2c7a96e162c7aae5fe0b7
|
|
| MD5 |
0efee65e1e175d23e263f08e94d8a09a
|
|
| BLAKE2b-256 |
e68a25c04682093a1d465dbd212493a8468be4f8086b1bdef5d49ca3fc3e6dfe
|
File details
Details for the file django_admin_reversefields-0.2.0-py3-none-any.whl.
File metadata
- Download URL: django_admin_reversefields-0.2.0-py3-none-any.whl
- Upload date:
- Size: 15.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d2905b52ea2e5cc1c61829920985667fddc4f2ade2da9f16d5df62fa331fd95
|
|
| MD5 |
b0d7bb45bdcee90086ba0a47144c7acf
|
|
| BLAKE2b-256 |
03f53134793b33bccb4ea25d6e4d620cfd3dd1038a59fb10ac8a0c0308a3cb5b
|