keeping filter states after edits

 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

  1. Keeping filter states after edits (Django 1.4) by rhfung 1 year, 5 months ago
  2. Admin: return to change_list with filter and pagination applied by fx 3 years ago
  3. Return to change_list with filter after change by graveyboat 2 years, 8 months ago
  4. Return to a filtered changelist on change form save by richardbolt 3 years, 8 months ago
  5. Custom change_list filter based on SimpleListFilter shows only referenced (related, used) values by darklow 1 year, 2 months ago

Comments

tinti (on September 26, 2011):

Ho to apply this snippet to achieve the functionality on every Admin model configured?

#

glarrain (on July 22, 2013):

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

#

(Forgotten your password?)