Skip to content

Filtering two different model fields #1427

Answered by rpkilby
pljspahn asked this question in Q&A
Discussion options

You must be logged in to vote

The form uses __str__ method of the Genus model to display the common genus instead of the Latin genus

By default, choice labels are generated using __str__. To change this, you'll need to implement label_from_instance as described at the very bottom of the ModelChoiceField docs. Then, you'll need to provide the field_class to your ModelMultipleChoiceFilter.

It just doesn't filter correctly, returning all objects

This will still depend on your view, but I can at least say that you don't need to have two separate filterset classes. genus__common and genus__latin can be members of the same filterset.

Optionally, I'd like to change the ordering for LatinFilter so that they order by lati…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by carltongibson
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1198 on August 26, 2021 14:57.