Skip to content

Commit

Permalink
Use subquery for related filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan P Kilby committed Jan 10, 2018
1 parent be308b7 commit eb652e0
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion rest_framework_filters/filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from collections import OrderedDict
from contextlib import contextmanager

from django.db.models import Subquery
from django.db.models.constants import LOOKUP_SEP
from django.http.request import QueryDict
from django_filters import filterset, rest_framework
Expand Down Expand Up @@ -270,6 +271,7 @@ def filter_related_filtersets(self, queryset):
"""
for field_name, related_filterset in self.related_filtersets.items():
lookup_expr = LOOKUP_SEP.join([field_name, 'in'])
queryset = queryset.filter(**{lookup_expr: related_filterset.qs})
subquery = Subquery(related_filterset.qs.values('pk'))
queryset = queryset.filter(**{lookup_expr: subquery})

return queryset

0 comments on commit eb652e0

Please sign in to comment.