django-object-actions icon indicating copy to clipboard operation
django-object-actions copied to clipboard

Preserve List Filters

Open WarToaster opened this issue 8 years ago • 2 comments

Is there a way to redirect an user after he clicked on an action and preserve applied filters?

Any help would be appreciated :)

WarToaster avatar Sep 16 '17 17:09 WarToaster

I don't think that's something that can be handled with django-object-actions. You could store Admin form state in localStorage.

crccheck avatar Oct 17 '17 18:10 crccheck

Thanks for the work on it, but it doesn't quite seem fixed. The newest Pypi release still got the old templates files, and all list filters still get dropped anyway.

Here's my current workaround:

  • add custom templates
change_form_template = "django_object_actions/change_form_filters.html"
change_list_template = "django_object_actions/change_list_filters.html"
  • add custom redirect function (mostly copied from django source contrib/admin/options.py)
def redirect_preserve_filters(self, request, obj, change_list=False):
        opts = self.model._meta
        preserved_filters = self.get_preserved_filters(request)
        if change_list:
            redir_url = reverse('admin:%s_%s_changelist' %
                                (opts.app_label, opts.model_name),
                                current_app=self.admin_site.name)
        else:
            pk_value = obj._get_pk_val()
            redir_url = reverse('admin:%s_%s_change' %
                                (opts.app_label, opts.model_name),
                                args=(pk_value,),
                                current_app=self.admin_site.name)
        preserved_filters = self.get_preserved_filters(request)
        redir_url = add_preserved_filters({'preserved_filters': preserved_filters, 'opts': opts}, redir_url)
        return HttpResponseRedirect(redir_url)
  • call that function at the end of every object action
...
obj.save()
return self.redirect_preserve_filters(request, obj)

Please tell me if I got that wrong, and thanks again!

WarToaster avatar Apr 25 '18 18:04 WarToaster