Django-filters resets after using the UpdateView

I have a Model with a lot of entries, so I'm using django-filters to filter the model, I initially load an empty table and from there I use the filter to view the items.

Everything works fine, the page loads initially with no entry, after I filter, django shows the correct items. The Url gets a parameter: /retetabloc/?acordcadru=532(532 is the filter) but when I try to update an entry, the filter resets(the parameter is still in the URL) and the whole db is loaded.

I don't quite understand how to pass the filter parameter to the RetetaBlocUpdate, so that after the update is done it returns to the filtered items like in the ListView.

views.py

class RetetaBlocListview(LoginRequiredMixin, CoreListView):
    model = RetetaBloc       
    def get_queryset(self, *args, **kwargs):
        pdb.set_trace()
        acordcadru = self.request.GET.get("acordcadru")
        queryset = RetetaBloc.objects.filter(acordcadru=acordcadru)
        return queryset

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['filter'] = RetetaBlocFilter(self.request.GET, queryset=self.get_queryset())
        pdb.set_trace()
        return context

class RetetaBlocUpdate(LoginRequiredMixin, AjaxUpdateView):
    model = RetetaBloc
    form_class = RetetaBlocForm

Thank you.

If you'd like filters to be remembered you could add them to a session variable instead. That way filters would be recalled even if they didn't go back directly from the update page (and you'd wouldn't have redundant URL querystring on pages where they weren't needed). Something like:

def get_queryset(self, *args, **kwargs):
    pdb.set_trace()
    #check for new filter in URL first
    acordcadru = self.request.GET.get("acordcadru")
    #if nothing check for session variable
    if not acordcadru:
        acordccadru = self.request.session.get('acrodcadru')
    #if something in URL querystring, set it in session variable
    else:
         self.request.session['acordcadru'] = acordcadru
    queryset = RetetaBloc.objects.filter(acordcadru=acordcadru)
    return queryset
Back to Top