Skip to main content

A Django mixin for easily including reverse relationships in your changeforms in the admin

Project description

django-admin-reversefields

PyPI version PyPI downloads Python versions Django versions Docs

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


Download files

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

Source Distribution

django_admin_reversefields-0.2.0.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

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

django_admin_reversefields-0.2.0-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file django_admin_reversefields-0.2.0.tar.gz.

File metadata

File hashes

Hashes for django_admin_reversefields-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0a5fc635fd1e0099fe48215670556b05944bd0305ed2c7a96e162c7aae5fe0b7
MD5 0efee65e1e175d23e263f08e94d8a09a
BLAKE2b-256 e68a25c04682093a1d465dbd212493a8468be4f8086b1bdef5d49ca3fc3e6dfe

See more details on using hashes here.

File details

Details for the file django_admin_reversefields-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_admin_reversefields-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6d2905b52ea2e5cc1c61829920985667fddc4f2ade2da9f16d5df62fa331fd95
MD5 b0d7bb45bdcee90086ba0a47144c7acf
BLAKE2b-256 03f53134793b33bccb4ea25d6e4d620cfd3dd1038a59fb10ac8a0c0308a3cb5b

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