drf与layui列表数据查询当月或指定月份的数据
class JsonTest(APIView):
def get(self, request, *args, **kwargs):
if 'month' in request.query_params: # 判断是否传参month:2021-03
time_month = request.query_params['month'].split('-') # 切割年月
# 查询范围某年某月garbage_day__year=time_month[0], garbage_day__month=time_month[1]
obj_garbage = Garbage.objects.filter(garbage_day__year=time_month[0], garbage_day__month=time_month[1])
else: # 如果没有传参则为默认当月数据
now = datetime.now()
obj_garbage = Garbage.objects.filter(garbage_day__year=now.year, garbage_day__month=now.month)
serializer = GarbageSerializer(instance=obj_garbage, many=True)
print(serializer.data)
return Response({
"code": 0,
"data": serializer.data,
})