Last active
June 27, 2022 14:46
-
-
Save tinytengu/7aab838b38d15ebc2c6958ceb7061955 to your computer and use it in GitHub Desktop.
Miltiple field lookup mixin for Django Rest Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import get_object_or_404 | |
class MultipleFieldLookupMixin: | |
lookup_ignorecase = True | |
def get_object(self): | |
queryset = self.get_queryset() | |
queryset = self.filter_queryset(queryset) | |
filter = { | |
"{}{}".format( | |
field, "__iexact" if self.lookup_ignorecase else "" | |
): self.kwargs[field] | |
for field in self.lookup_fields | |
if self.kwargs.get(field) | |
} | |
obj = get_object_or_404(queryset, **filter) | |
self.check_object_permissions(self.request, obj) | |
return obj |
Author
tinytengu
commented
Jun 27, 2022
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment