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