Django Admin - RelatedObjectLookups - How to update this and set the selection in the parent window?

I want one of my forms to work the same as the admin page, so I decided that I would look at the code and see how it works.

In particular, I want the user to be able to click the β€œ+” sign next to the selection list and go to the pop-up form of the admin page to add a new item.

When they introduce a new item, I want the new item to appear in the selection box and be selected (how and how this function works on the admin pages).

I copied the js-jsys libraries into my own template, and I made the link a link to the same JS function, and the pop-up windows open correctly, but after saving the new object, the pop-up window becomes empty, not closed, and nothing happens on the parent page.

Here is what I put on my page:

... <td> <div class="fieldWrapper"> <select name="form-0-plasmid" id="id_form-0-plasmid"> ... </select> <a href="/admin/VirusTracker/plasmid/add/" class="add-another" id="add_id_plasmid" onclick="return showAddAnotherPopup(this);"> <img src="/media/admin/img/admin/icon_addlink.gif" width="10" height="10" alt="Add Another"/></a> </div> </td> ... 

I tried going through javascript in the admin form to see how it works, but I don't see anything that could close the window or fill the parent window.

Thanks in advance for your help.

Update 3

I get this javascript error when running offsetAddAnotherPopup function

 "SelectBox is not defined" 

What is indicated on this line in the offsetAddAnotherPopup function

 SelectBox.add_to_cache(toId, o); 

I thought I knew Javascript, but I do not see where this variable should come from: - (

Update 2

Everything seems to be working correctly. After I click "Save" in the popup window, I get a blank page. This is the source of this page:

 <script type="text/javascript">opener.dismissAddAnotherPopup(window, "9", "CMV_flex_myr_GENE1_._._WPRE_BGH");</script> 

So it seems that this javascript is not executing or not working.

Update

Here is the corresponding code mentioned by Daniel. Therefore, the only problem is that this code either fails or fails to work correctly.

Django / vno / admin / options.py:

 ... if request.POST.has_key("_popup"): return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \ # escape() calls force_unicode. (escape(pk_value), escapejs(obj))) ... 

/media/admin/js/admin/RelatedObjectLookups.js:

 function dismissAddAnotherPopup(win, newId, newRepr) { // newId and newRepr are expected to have previously been escaped by // django.utils.html.escape. newId = html_unescape(newId); newRepr = html_unescape(newRepr); var name = windowname_to_id(win.name); var elem = document.getElementById(name); if (elem) { if (elem.nodeName == 'SELECT') { var o = new Option(newRepr, newId); elem.options[elem.options.length] = o; o.selected = true; } else if (elem.nodeName == 'INPUT') { if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) { elem.value += ',' + newId; } else { elem.value = newId; } } } else { var toId = name + "_to"; elem = document.getElementById(toId); var o = new Option(newRepr, newId); SelectBox.add_to_cache(toId, o); SelectBox.redisplay(toId); } win.close(); } 
+3
source share
3 answers

Well, javascript just uses the id attribute of the launch element to determine the select box to update. (after removing "add_" from beginig).

So, I just changed the id attribute of the link to match the identifier of the select element in my template:

 <a href="/admin/VirusTracker/plasmid/add/" class="add-another" id="add_id_{{field.html_name}}" onclick="return showAddAnotherPopup(this);"> <img src="/media/admin/img/admin/icon_addlink.gif" width="10" height="10" alt="Add Another"/></a> 

Wow, I would like it to be documented somewhere! I lost a few hours on this.

(See my updates to the question for more technical details on how this all works.)

+5
source

The trick - and this is a bit of a hack, in fact - is what happens when you click save on the admin popup.

If you look at the response_add code in django.contrib.options.ModelAdmin , you will see that when you save the item in a pop-up window, the administrator returns an HttpResponse consisting solely of the Javascript part. This JS calls the dismissAddAnotherPopup function in the parent window, which closes the popup and sets the form value accordingly.

Just copy this functionality into your own application.

Edited after updates. If the javascript administrator is not working, it usually depends on the fact that it depends on the jsi18n code that you specify through the URL (and not the static path):

 <script type="text/javascript" src="/admin/jsi18n/"></script> 
+2
source

I had the same problem that was updating the selection in the parent window and I decided to follow this doc

Everything works fine now

Change 1:

I tried to use Select2 to make a nice choice, the only choice works fine, multiple choice gives me headaches, for some reason does not update the information in the parent form.

Has anyone tried this before?

0
source

All Articles