-
Notifications
You must be signed in to change notification settings - Fork 770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Weird Behaviour when filtering Model objects by a related model #1672
Comments
I have an update to this, take a look at the example below:
The above returns nothing and it appears that your algorithm searches through an Authors books and returns the Author if at least one book contains "othello" in the title AND at least one book is a "comedy". I.e. all the filters we apply must contained by the fields of our books in a non-intersecting manner. Since none of the Books we have for this author is a comedy, the author will not be returned. This makes sense but I would still like to have my original method. Is there a way to do this? |
This is standard Django behaviour. See the Spanning multi-valued relationships docs. If you need to filter in a single step, define a filter that takes both fields from the query data and handles those together. There's an example of that here: https://gitlab.com/-/snippets/2237049 |
Thank you for that. Taking all of this into account, I have added this filtering method to the FilterAuthorByBook class:
It appears to filter items now as I intended. |
Lets say we have an app called:
filter
with 2 models:Author
andBook
where anAuthor
can have manyBook
objects.We also have defined the filter here, where we can filter the
Author
based ontitle
andgenre
of their books:Now lets create an example
We would like to filter authors by the book title "othello" and the "history" book genre. I would expect that nothing would get returned, but it appears that it is filtering the author based on if their book field matches at least one filter we applied, whereas I want the author's books to match all applied fields, for an author to be returned.
Is this the intended behaviour? How can I change it such that the author's books must match all applied fields, for an author to be returned.
The text was updated successfully, but these errors were encountered: