Now I have the following logic implemented for a GET
request in Django Rest Framework
:
class SomeViewSet(mixins.ListModelMixin,
GenericViewSet):
count = None
def get_queryset(self):
query_set = ... # some_logic
self.count = query_set.count()
return query_set
def list(self, request, *args, **kwargs):
response = super().list(request, *args, **kwargs)
response.data = {'count': self.count,
'data': response.data}
return response
That is, the queryset
is calculated according to complex logic, and it may contain a different number of objects that need to be returned in a GET
request, since I don’t have access to the query_set
variable inside the list function and I don’t want to copy the query_set
calculation logic, I decided do it with a class variable.
But still, the feeling that this is not very correct does not leave. What other options are there?