Skip to content

Commit da7ce00

Browse files
authored
Merge pull request #47 from namespace-ee/support-for-list-types
Add support for lists in filter_related_objects
2 parents 3854fa9 + 239f1b5 commit da7ce00

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

drf_sideloading/mixins.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,18 @@ def get_sideloadable_page(self, page):
179179

180180
def filter_related_objects(self, related_objects, lookup):
181181
current_lookup, remaining_lookup = lookup.split("__", 1) if "__" in lookup else (lookup, None)
182-
related_objects_set = {getattr(r, current_lookup) for r in related_objects} - {None}
183-
if related_objects_set and next(iter(related_objects_set)).__class__.__name__ in [
184-
"ManyRelatedManager",
185-
"RelatedManager",
186-
]:
187-
related_objects_set = set(chain(*[related_queryset.all() for related_queryset in related_objects_set]))
182+
lookup_values = [getattr(r, current_lookup) for r in related_objects if getattr(r, current_lookup) is not None]
183+
184+
if lookup_values:
185+
if lookup_values[0].__class__.__name__ in ["ManyRelatedManager", "RelatedManager"]:
186+
related_objects_set = set(chain(*[related_queryset.all() for related_queryset in lookup_values]))
187+
elif isinstance(lookup_values[0], list):
188+
related_objects_set = set(chain(*[related_list for related_list in lookup_values]))
189+
else:
190+
related_objects_set = set(lookup_values)
191+
else:
192+
related_objects_set = set()
193+
188194
if remaining_lookup:
189195
return self.filter_related_objects(related_objects_set, remaining_lookup)
190196
return set(related_objects_set) - {"", None}

0 commit comments

Comments
 (0)