from rest_framework.viewsets import ReadOnlyModelViewSet
from drf_renderer_xlsx.mixins import XLSXFileMixin
from drf_renderer_xlsx.renderers import XLSXRenderer
from .models import MyExampleModel
from .serializers import MyExampleSerializer
class MyExampleViewSet(XLSXFileMixin, ReadOnlyModelViewSet):
serializer_class = MyExampleSerializer
renderer_classes = [XLSXRenderer]
filename = 'my_export.xlsx'
def get_queryset(self):
start_date = self.request.query_params.get('start_date', None)
end_date = self.request.query_params.get('end_date', None)
queryset = MyExampleModel.objects..filter(created__range=[start_date, end_date])
Return queryset
# What I want to do
# If not queryset:
# Return Response({"message": "Exporting Fail"})
# Is there a way to check if queryset is None and return a Error Message instead of an empty Excel
# I think that I not allow return Response in the get_queryset function
Currently, I am trying to build a function to export excel file. I just want to know is there a way to check if the queryset is None and then I can return a Response({"message": "Exporting Fail, Empty"})
If you know where can I research it would help me a lot. Thank you so much
You should be doing the filtering in
filter_querysetorget_querysetso what you already have is correct. Butget_querysethas to return aqueryset, not aResponseobject.You will access the actual object in
get_objectmethod.If you don't want to raise a 404 if queryset is empty, you will have to first adjust those few final lines.
Then, as you can only
retrieveandlistin your view set, adjust those methods accordingly.Or you don't alter
get_objectat all and handle 404but imo it's not a clean solution.