By default every time you change and save an object in the admin, the change_list "jumps" to the first page, so filters you used to find the object (or the pagination-page) have to be applied again. If you have to go through a multi-object-list step-by-step this could become really annoying.
The above snippet changes this behaviour by returning to the referring URL when saving. Included in this URL are variables for the filters/pagination.
The snippet is part of your custom Model.admin in admin.py.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class DocumentAdmin(admin.ModelAdmin):
def change_view(self, request, object_id, extra_context={}):
"""
save the referer of the page to return to the filtered
change_list after saving the page
"""
if len(extra_context) > 0:
"""
second call of the form (after saving), take the
referer from extra_context['ref'], clear extra_context
and put the referer in result['Location']
"""
ref = extra_context['ref']
result = super(DocumentAdmin, self).change_view(request, object_id, extra_context.clear() )
result['Location'] = ref
else:
"""
first call of the form: add the referer to the context
"""
extra_context['ref'] = unicode( request.META.get('HTTP_REFERER', '') )
result = super(DocumentAdmin, self).change_view(request, object_id, extra_context )
return result
admin.site.register(Document, DocumentAdmin)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
This is really useful. I have some links to the admin change_view in my frontend and i was looking for an easy redirection (back to the frontend) after an object is saved. This seems to to do it.
How would i easily provide the enhanced change_view for all my ModelAdmins. Can i easily subclass ModelAdmin, change the change_view and make my models use MyModelAdmin? Or will i run into trouble doing this ... ?
#
Ditto re arie's comment - I think subclassing, or even a mixin is the way to go ... but what are the "side effects"?
#
Follow-up: the code, as at 14 April 2011, would need to be changed as it refers to "DocumentAdmin" i.e. it is not actually generic code and so could not be used via a subclass/mixin approach...
#
This snippet works when using the built-in Django server, but fails when running with Apache & WSGI.
Using Django 1.3, Python 2.6.5
Was able to run a debugger, saw both branches of the if statement fire when I clicked on a link in the changelist, then when I changed and saved the record, the both fired again. So instead of firing once when I drilled down from the change list (save the location and filter info) and once again to retrieve the location & filter info, it does that cycle twice on each click and just brings the unfiltered changelist back.
Any ideas?
#
Oh, uh, it seems to be working with Apache now. Maybe it always did. But the save/restore of the extra context can get out of whack with certain navigation patterns I haven't nailed down to a deterministic test case yet, and then the unfiltered changelist appears upon save.
#
Please login first before commenting.