-
Notifications
You must be signed in to change notification settings - Fork 767
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
Filtering reverse relations' behaviour #883
Comments
Could you provide the url with the query? |
Same issue here: models.py (the intermediate model containing the foreign keys):
Filtering in the shell:
Filtering with this FilterSet (instead of methods used by OT this is using the name and distinct attr of CharFilter):
brings this result:
^^ Please don't care about the schema |
I have the same issue. I have asked a question similar to this problem and provide some examples and code to show this problem. Also, I have created an example project that shows this problem in a basic example. |
Should be addressed by #915 |
I’m just going to reopen this one to review. |
It seems like it is django queryset filters issue: |
We just encountered this in our project as well. I'm wondering what's the potential solution - could we use one |
@pawelad That could help, but you need a custom filter method for that 🙂 You may found this article useful - https://hacksoft.io/django-filter-chaining/ |
I don't think a custom filter method is needed. For example, this is what a colleague of mine did: class ManyToOneFilterSet(FilterSet):
def filter_queryset(self, queryset):
"""
Overrides the basic methtod, so that instead of iterating over tthe queryset with multiple `.filter()`
calls, one for each filter, it accumulates the lookup expressions and applies them all in a single
`.filter()` call - to filter with an explicit "AND" in many to many relationships.
"""
filter_kwargs = {}
for name, value in self.form.cleaned_data.items():
if value not in EMPTY_VALUES:
lookup = "%s__%s" % (self.filters[name].field_name, self.filters[name].lookup_expr)
filter_kwargs.update({lookup: value})
queryset = queryset.filter(**filter_kwargs)
assert isinstance(queryset, models.QuerySet), (
"Expected '%s.%s' to return a QuerySet, but got a %s instead."
% (type(self).__name__, name, type(queryset).__name__)
)
return queryset |
Hi, I hit a problem when filtering by multiple fields from a reverse relation at once. If I have the following models:
And the following filter:
So if I filter by
country_name
andgroup_title
I expected to get the actors who have followers BOTH fromcoutry with name=<some_value>
AND fromgroup with titile=<some_value>
. But instead I got actors who either has followers fromcountry with name=<some_value>
or has followers from group withtitile=<some_value>
. So there was duplicated results.Reading the django docs I found this explanation for filtering by reverse relations - https://docs.djangoproject.com/en/2.0/topics/db/queries/#lookups-that- .
So I was wondering if I'm using
django-filters
right.Is this the expected behaviour(having duplicated values when filtering by multiple reverse relation) or it is a bug?
The text was updated successfully, but these errors were encountered: