Django formset class view

Hello,

I have a listview where a define a form and formset. The form is used to filter a table and it works.

I try to create a formset, with 3 input values( one from models, and other custom ).

When I test, I filter only with the last form filled. How can I access to all fieldS values ?

forms.py:

class CmsFilterForm(ModelForm):
    class Meta:
        model = Cmsexport
        #fields = ['name', 'state_text']
        fields = '__all__'

CmsFormSet = modelformset_factory(
    Cmsexport,
    fields = '__all__',
    extra=1
)

class DnsFilterForm(ModelForm):
    class Meta:
        model = Dns
        fields = '__all__'

class AliasFilterForm(ModelForm):
    class Meta:
        model = Alias
        fields = '__all__'

class SectoolsFilterForm(ModelForm):
    class Meta:
        model = Sectools
        fields = '__all__'

views.py

class CmsView(ListView):
    queryset = Cmsexport.objects.all().values()
    model = Cmsexport
    form_class = CmsFilterForm
    template_name = 'table.html'
    context_object_name = 'table'
    paginate_by = 10

    def get_queryset(self):
        queryset = super(CmsView, self).get_queryset()
        field = self.request.GET.get('field')
        lookup = self.request.GET.get('lookup')
        value = self.request.GET.get('value')
        reset = self.request.GET.get('btn-reset')

        if (field and lookup and value) is not None:
            query = field.lower().replace(" ", "_") + '__' + lookup
            queryset = Cmsexport.objects.filter(**{ query: value }).values()

        if reset:
            queryset = Cmsexport.objects.filter().values()

        return queryset

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        #context['form'] = CmsFilterForm()
        context['formset'] = CmsFormSet(queryset=Cmsexport.objects.none())
        return context

and what I want is this:

[14/Sep/2023 13:08:52] "GET /cms/?csrfmiddlewaretoken=E2kPYhfkP4hHNd13u7tQUUVCNTP2M0uhrxnjcPu1hNimxMdszJXFOTLXPjGhsiNL&form-TOTAL_FORMS=2&form-INITIAL_FORMS=0&form-MIN_NUM_FORMS=0&form-MAX_NUM_FORMS=1000&field=Name&lookup=icontains&value=vautour&field=State+text&lookup=iexact&value=removed HTTP/1.1" 200 119433

especially this →

field=Name&lookup=icontains&value=vautour&field=State+text&lookup=iexact&value=removed

any idea?

Regards,

First, I’m not sure I’m clear on your overall objective here - I may be misunderstanding what you’re saying, so what I’m saying here may not apply.

Anyway, as a general comment - whenever I’m creating a view with a combination of data and forms, I find it works better as looking at it as a “FormView with additional data to be displayed” and not a “DetailView (or ListView) with a form attached”. The flow of information just seems to be “cleaner” / “easier” to me.

Also, I don’t see the value in submitting those forms via GET instead of POST - but I recognize the potential value either way. Using GET is fine provided you don’t end up producing an overly-long URL and you’re not planning on saving any of the submitted data. (Side note: There’s no value in submitting the csrf token on a GET - a GET is supposed to be considered “safe” and not needing protection.)

If you’re working with a formset, you’ll want to work with formsets. That means binding the data that is submitted back to the formset - only in this case using request.GET instead of request.POST. Once you’ve bound the submitted data, you have access to the individual form instances with the data that was submitted - exactly like any other form.

Hello Ken,

In resume I need to show a table with data, then I try to create a form with 3 input, to filter the data table.
And I would like to create a formset to let the user filter the table like this:

name__contains=foo
state_text_exact=removed

I don’t need to save the form, and yes POST isn’t needed here, GET would be the best in this case.

Ok, I’m with you so far, but:

A formset is a data structure representing a list of identical form classes.

I don’t see why you’re trying to use a formset here.

Because the tsble(db) contains a lot of column, if I show all field in one time, the user experience would be bad, becauce the user need to scroll all input filed ( 52 in this case) , filled some of them and then scroll down to click the submit button.

I would have one form with a button add form like this:

Name contains value
State exact value
etc

I understand why you’re trying to use a filter for the data being displayed. That’s not what I’m questioning.

What I’m asking about is why you’re using a formset for the data that is going to be entered for the filter. What you’re describing to me is one form for a filter. Not a formset.

I’m starting to get the impression that there may be some terminology issue here where we’re using the same words for two different things.

Since I’m really not getting what you’re trying to accomplish here, it might be helpful at this point if you could provide a mockup or image of what you’re trying to build with a more detailed explanations of what’s going to be on the page and how it’s supposed to act.