- Author:
- chronosllc
- Posted:
- August 25, 2011
- Language:
- Python
- Version:
- 1.3
- Score:
- 2 (after 2 ratings)
When saving an edit to an object from a filtered list view you are, by default, returned to list view without any of your filters applied.
This solves that problem, keeping the filtered view in a session variable until you reach a point where the session key is deleted.
The solution presented here is hugely based off of other's work with most of the solution gained from: Admin: return to change_list with filter and pagination applied
This solution offered the best approach in our mind over the others listed here on snippets since the solution didn't require changes to template code...this is completely self contained within your own admin.py files.
The advantage to our solution over the above linked solution is that under different use cases the user may or may not be redirected to the filtered list_view. For example, if you edit an object and click the save and continue button, then you would lose the filter when you finally finished editing the object and clicked save.
Added on here is a delete of the session key when users add objects, the reasoning we're going this route is we don't want to return users to filtered views when they just added a new object. Your mileage may vary and if so, it's easy enough to fit your own needs.
HTHs
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 27 28 29 30 31 32 33 34 35 36 37 38 39 | def add_view(self, request, *args, **kwargs):
result = super(LocationAdmin, self).add_view(request, *args, **kwargs )
"""
Delete the session key, since we want the user to be directed to all listings
after a save on a new object.
"""
request.session['filtered'] = None
return result
"""
Used to redirect users back to their filtered list of locations if there were any
"""
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
"""
result = super(LocationAdmin, self).change_view(request, object_id, extra_context )
# Look at the referer for a query string '^.*\?.*$'
ref = request.META.get('HTTP_REFERER', '')
if ref.find('?') != -1:
# We've got a query string, set the session value
request.session['filtered'] = ref
if request.POST.has_key('_save'):
"""
We only kick into action if we've saved and if
there is a session key of 'filtered', then we
delete the key.
"""
try:
if request.session['filtered'] is not None:
result['Location'] = request.session['filtered']
request.session['filtered'] = None
except:
pass
return result
|
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
Ho to apply this snippet to achieve the functionality on every Admin model configured?
#
Note that this feature was long requested (the ticket is 5 years old) bit it has finally been added to the trunk so we can expect it for Django 1.6. https://code.djangoproject.com/ticket/6903
#
Please login first before commenting.