Skip to content
Automatically update multiple foreign keys in the Django admin
Python JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
multi_fk
.gitignore
LICENSE.txt
MANIFEST.in
README.md
setup.py

README.md

django-multi-fk

License PyPI Version PyPI Downloads

In order to explain what this package does and the problem it solves, consider the following models:

class Something(models.Model):
    pass

class Someone(models.Model):
    thing1 = models.ForeignKey(Something, related_name='+')
    thing2 = models.ForeignKey(Something, related_name='+')

Registering both of these models in the Django admin results in the following <select> fields:

Django Admin

Clicking the "+" next to "Thing1" causes a popup window to open which can be used to add a new item. Once the popup is dismissed, the new item appears in the <select> for "Thing1" but not in the <select> for "Thing2". This is the problem that django-multi-fk attempts to solve.

Installation

The easiest way to install the package is by using:

pip install django-multi-fk

Once installed, simply add multi_fk to INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    #...
    'multi_fk',
]

How It Works

The app uses some clever monkey-patching to modify the behavior of the admin site. There are two changes made:

  • a data-model HTML5 attribute is added to <select> elements on the page to indicate which model is being displayed
  • JavaScript on the page updates the <select> elements belonging to the same model when one is changed
You can’t perform that action at this time.