Hi there
I’ve got a Model, and a ModelForm, and I’ve been experimenting with a view that includes a form at the top driving djanggo-filter and a table with the (filtered) query set below.
Initially, I set up a filters.py to define filters for 8 of the 9 fields on the model, including adding widgets and some attrs with DaisyUI classes. I included the 8 fields in the filter Meta class fields dict. It is all working (though I’m sure I can stop it working by trying to use htmx in the near future.)
I then discovered the option to specify a form in the filter Meta class. Specifying the ModelForm for the Model being filtered seemed to add constraints: 1) I had to rename some of the filters to match the fields from the Model to make the ModelForm happy (even though these were defined in filters.py with field_names and lookup_expr, where appropriate); and 2) I had to override the filter_queryset method on the Filter class to avoid an error because there was one field fewer than in the ModelForm. Furthermore, even though I had brought the form into the filter, I found that none of the widgets and DaisyUI classes in the form were inherited by the filters, so I ended up having to specify them again in filters.py anyway.
I had thought the better practice here would be to define as much at the ModelForm level as possible, bring that into the Filter and only add further content where necessary for the filtering. But, in trying to do so, I seem to have lost some flexibility, and not gained much benefit. Is this the better practice or have I missed something ? Would I get more benefit from drawing on the ModelForm if my filters were simpler and could be defined through the filter Meta class ?