django 解决queryset惰性机制,实现实时查询

django 在第一次查询后,就把数据进行缓存。如果对数据进行操作后,再进行查询时直接去缓存中取而不去数据库查询,对于想要实时数据时这并不友好。在百度后解决方案如直:

class TodayRecordView(viewsets.ModelViewSet):
    serializer_class = OrderRecordSerializer
    pagination_class = StandardResultsSetPagination
    # queryset = TestRecord.objects.raw(select_sql+"AND oo.carry_date = current_date", [0])

    def list(self, request, *args, **kwargs):
        response = super().list(request, *args, **kwargs)
        context = {
            'items': response.data['results'],
            'total': response.data['count'],
        }
        return APIResponse(results=context)

    def get_queryset(self):
        TestRecord.objects.update()
        return TestRecord.objects.raw(select_sql+"AND oo.carry_date = current_date", [0])

参考资料:
https://www.cnpython.com/qa/37683



来自为知笔记(Wiz)


posted on 2023-08-27 12:02  白衣风云  阅读(54)  评论(0编辑  收藏  举报

导航