Keeping filters in Django Admin

前端 未结 7 1007
走了就别回头了
走了就别回头了 2021-02-04 20:23

What I would like to achive is:

  • I go to admin site, apply some filters to the list of objects
  • I click and object edit, edit, edit, hit \'Save\'
7条回答
  •  情深已故
    2021-02-04 20:46

    In my opinion its better to override methods from ModelAdmin changelist_view and change_view:

    Like so:

    class FakturaAdmin(admin.ModelAdmin):
    
    [...]
    
    def changelist_view(self, request, extra_context=None):
        result = super(FakturaAdmin, self).changelist_view(request, extra_context=None)
        request.session['qdict'] = request.GET
        return result
    
    def change_view(self, request, object_id, extra_context=None):
        result = super(FakturaAdmin, self).change_view(request, object_id, extra_context)
        try:
            result['location'] = result['location']+"?"+request.session['qdict'].urlencode()
        except:
            pass
        return result
    

    As you wish, after save object you go back to list of objects with active filters.

提交回复
热议问题