How can I cache queryset (and invalidate cache) results in ListAPIView
when there is pagination, filtering and ordering possible? Note that I need to invalidate cache per company so if any new Item
object is created for a specific company I need to invalidate.
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
class CustomPaginationClass(PageNumberPagination):
page_size = "30"
page_size_query_param = 'perPage'
max_page_size = 10000
class ItemListApiView(ListAPIView):
model = Item
authentication_classes = (SessionAuthentication, )
serializer_class = ItemSerializer
permission_classes = (HasAccessToItems, )
pagination_class = CustomPaginationClass
filter_backends = (filters.SearchFilter, DjangoFilterBackend, filters.OrderingFilter, )
search_fields = ('user__name', )
filter_fields = {
'category_type__name': ['exact'],
'item_type__name': ['exact'],
'approver__name': ['exact'],
'status': ['exact', 'in']
}
ordering_fields = '__all__'
def get_queryset(self):
qs = Item.objects.filter(company=self.request.user.company).select_related('company')
if not self.request.user.is_all_items_enabled:
qs = qs.filter(approver=self.request.user)
return qs.order_by('-created')
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)