Last active
February 18, 2020 12:03
-
-
Save serafdev/12061546a9021832cc9be131f7195b4d to your computer and use it in GitHub Desktop.
This file contains hidden or 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.db.models import Q | |
# `/GET /items?name__startswith=Grap&price_gte=300&price__lte=700` | |
def get_queryset(self): | |
filters = { | |
"name__startswith": self.request.GET.get("name__startswith ", ""), | |
"price__gte": self.request.GET.get("price_gte ", ""), | |
"price__lte": self.request.GET.get("price__lte", ""), | |
} | |
def queries(filters): | |
return [Q(**{k: v}) for k, v in filters.items() if v] | |
return Item.objects.filter(*queries(filters)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment