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,
})
本文介绍了一种使用Django Rest Framework (DRF) 和 layui 技术结合的方法来查询指定月份或当前月份的数据。具体实现中,通过API视图接收请求参数,并依据参数查询数据库,最终返回特定月份的数据列表。
1126

被折叠的 条评论
为什么被折叠?



