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
目前,我正在尝试构建一个导出 excel 文件的函数。我只想知道有没有办法检查查询集是否为 None 然后我可以返回一个 Response({"message": "Exporting Fail, Empty"})
如果你知道我在哪里可以研究它会对我有很大帮助。太感谢了