-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqueryset_mixin.py
33 lines (27 loc) · 1.2 KB
/
queryset_mixin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""provides a common implementation of get_queryset method in viewset"""
from django.http import Http404
from ansible_catalog.main.models import Tenant
class QuerySetMixin:
"""
A Mixin class to be inherited by a customer ViewSet class
"""
def get_queryset(self):
"""filter by current tenant and query_lookup_key, order by queryset_order_by"""
parent_field_names = getattr(self, "parent_field_names", [])
queryset_order_by = getattr(self, "queryset_order_by", None)
serializer_class = self.get_serializer_class() or self.serializer_class
queryset = serializer_class.Meta.model.objects.filter(
tenant=Tenant.current()
)
for parent_field_name in parent_field_names:
parent_lookup_key = f"{parent_field_name}_id"
if parent_lookup_key in self.kwargs:
try:
queryset = queryset.filter(
**{parent_field_name: self.kwargs[parent_lookup_key]}
)
except ValueError:
raise Http404
if queryset_order_by is not None:
queryset = queryset.order_by(queryset_order_by)
return queryset