multiple OR arguments for non-model field

13 views
Skip to first unread message

IoannisL

unread,
Oct 24, 2019, 6:51:21 AM10/24/19
to django-filter
I need to filter against a UUID field using the following query:
?groupId=<UUID>&groupId=<UUID>

I have gone through github issues like https://github.com/carltongibson/django-filter/issues/137 but could not understand how to implement this using the framework.


This is the code I have:



2 from django_filters import rest_framework as filters 32 class CustomFilter(filters.UUIDFilter): 33 def filter(self, qs, value): 33 attr_name = next(k for k, v in self.parent.get_filters().items() if v.field_name == self.field_name) 34 if attr_name not in self.parent.data: 35 return super().filter(qs, value) 36 37 q = Q() 38 for val in self.parent.data.getlist(attr_name ): 39 q |= Q(**{f"{self.field_name}__{self.lookup_expr}": val}) 40 return self.get_method(qs)(q)



MultipleChoiceField seemed like a possible approach, but I'd have to build a choice list using a DB query, which doesn't sound like a good idea (caching wouldn't work as new IDs may be created at any time)


Am I missing some built in way of doing this filtering?  If not is the above something that might be beneficial to add to the library ? 

Reply all
Reply to author
Forward
0 new messages