Hi,
In the admin site when creating/updating an object A which has field “fk” which is a foreign key related to an object B (displayed in a select element) you can create/update objects B in a popup window.
I am wondering how to open this new window with Django. I did not find anything about this…
It can’t be done directly by Django. It has to be done using some JavaScript in the browser. (It can be done either directly in JavaScript, or using a library such as jQuery or HTMX.)
In the admin, the green plus sign triggers JavaScript to open a new window for the form.
You can find the code for what the admin is doing in django.contrib.admin.static.admin.js.admin.RelatedObjectLookups.js
I understand the javascript is very generic, allowing to open several popus in case a related object has its own related object.
I am now able to open the popup, but I have troubles regarding how to define the url to redirect to at the end of my creation view (generic CreateView) and to get the select choice up to date…
class Create_framework_agreement(CreateView):
model = Framework_agreement
fields=['number', 'company', 'signature_date']
I guess that the view has to handle “_to_field=id” and _“popup=1” variables…