This topic is fairly common (most explicitly detailed here: http://www.hoboes.com/Mimsy/hacks/replicating-djangos-admin/), but I'm still having trouble with it. I'm trying to use the "plus" button functionality used in the admin site where one can add an additional foreign key to a linked entry. In the admin site, a popup displays allowing the user to submit a new field and then that new value is populated on the original form.
I think my issue centers around the inclusion of this line:
within the base.html template and the popadd.html template. Clicking the plus button does not bring up a new window. The popadd template simply loads in the same tab. And submitting a new entry does not take the user back to the original form.
The admin site is functional. I am including ADMIN_MEDIA_PREFIX = '/media/admin/' in the settings.py file. Does it have something to do with where the RelatedObjectLookups.js lives? It's currently in an admin directory outside of my project folder. Do I have to create a symlink?
Sorry for the noob questions. Would appreciate any suggestions (as detailed as possible).
I've also created an app you can just include in your project at http://github.com/sontek/django-tekextensions
Following the steps outlined below will allow you to recreate Django admin's related object pop-up functionality without having to create any custom widgets, views, and urls. These steps assume you are trying to get this pop-up working in your own custom admin site which subclasses Django's admin.
Lets assume the following two models Book and Author, with an FK from Book to Author. Lets also assume that we'll want the ability to use the Related Object Pop-Up to add an Author when creating/editing a Book:
[app_name]/models.py:
Lets create our custom admin site:
[app_name]/sites.py:
Our custom admin site will register two ModelAdmins to allow users to add/edit/delete both the Book and Author models:
[app_name]/admin.py:
Now, we'll setup the
BookForm
which is used in theBookModelAdmin
above. This is where the magic happens. For more info on the RelatedFieldWidgetWrapper api, click here:[app_name]/forms.py:
Notes:
admin/js/core.js
andadmin/js/admin/RelatedObjectLookups.js
.Gotchas:
is_popup
needs to be set and passed correctly in your templates. Specifically, in any customchange_form.html
templates you override, you must remember to add this line somewhere within your form tags:{% if is_popup %}<input type="hidden" name="_popup" value="1" />{% endif %}
, so that the logic inBaseModelAdmin.response_add()
returns the correct response.Under The Hood: Essentially, we're re-using the form processing logic, widget wrapper and javascript that is already included with Django admin.
RelatedFieldWidgetWrapper
to wrap the widget associated to the related object field in our form (and specifically passingcan_add_related=True
in the constructor) tells the widget to append the necessary '+' link with the appropriate javascript onclick event attached to it.{% if is_popup %}...{% endif %}
logic in ourchange_form.html
template(s) and the logic inBaseModelAdmin.response_add()
handles the saving of the new related object and returns the appropriate javascript response that informs the pop-up that it needs to be closed.Related Repo: This public repo should provide sample code for the Django project discussed above: https://github.com/cooncesean/Books
If in the admin you want to display related objects in modals instead of old popup windows I suggest you to try the
django-admin-interface
.To install it follow these steps:
pip install django-admin-interface
admin_interface
,flat_responsive
andcolorfield
tosettings.INSTALLED_APPS
beforedjango.contrib.admin
python manage.py migrate
python manage.py collectstatic
For more detailed info, see django-admin-interface on GitHub.
Google pointed me to this page when searching how to get a "+" icon next to fields in a custom form with ForeignKey relationship (just like the admin site would do), so I thought I'd add.
For me, using
django-autocomplete-light
did the trick very well, using the "add another" functionality. See this live demo.See Django ModelChoiceField has no plus button as well.