I am writing code to open a new window inside the django admin to add a model instance and to close on save. This is very similar to the behaviour of ForeignKey field add (green plus sign) does, but without selecting the newly created model instance (because its not a foreign key field).
The code I add to make the pop-up link is:
link = '<a id="add_id_event" class="add-another" onclick="return showAddAnotherPopup(this);" href="%s?date=%s">add</a>' % ( addurl,currentdate)
where my model is called Event. I correctly add RelatedObjectLookups.js
When I try to save this model, django applies the same code it would use on a ForeignKey field and tries to activate a SelectBox which I don’t have. This causes the javascript to fail before it gets to the window.close()
I’ve tried overriding the save_model function with
def save_model(self, request, obj, form, change):
if request.GET.get('_popup') == '1':
obj.save()
return HttpResponse('<script type="text/javascript">window.close()</script>')
This code is used but the HttpResponse call is ignored and django renders the default. e.g.
<script type="text/javascript">opener.dismissAddAnotherPopup(window, "14382", "TMC 2012\u002D02\u002D02 10:00:00 DDT2010B\u002D028");</script>
which fails because there is no destination SelectBox object.
Thanks for your help.
You’ll need to override ModelAdmin.response_add. That’s where the redirect is happening.
In my case, I needed to override the dismissAddAnotherPopup method, so I created a new one called dismissAddAnotherPopupWithUpdate to handle my fancy M2M widgets. Here’s the code i used: