如何根据django-rest-framework中的某些参数获取最新记录



我正在尝试根据参数获取latest记录,但当使用(latest(), last(), earliest(), order_by('-id')[0], latest('-id'))所有这些方法在查询集然后我得到空记录,请帮助我。

@api_view(['GET'])
@permission_classes((IsAuthenticated,))
def details(request):
if request.method == 'GET':
category=request.query_params.get('category')
name=request.query_params.get('name')
try:
d=Employee.objects.filter(category=category,name=name).latest()
response_data=EmployeeSerializer(d,many=True).data
return JsonResponse({"message": "detail retrieval","error":False,"code":200,"results":{"totalItems":d.count(),"pageData":response_data,"totalPages":1,"currentPage":0}}, status=HTTP_200_OK)
except:
return JsonResponse({"message": "details not exist","error":True,"code":200,"results":{}}, status=HTTP_200_OK)
@api_view(['GET'])
@permission_classes((IsAuthenticated,))
def details(request):
if request.method == 'GET':
category=request.query_params.get('category')
name=request.query_params.get('name')
try:
d=Employee.objects.filter(category=category,name=name).latest('id')
serializer=EmployeeSerializer(d)
return JsonResponse({"message": "detail retrieval","error":False,"code":200,"results":{"totalItems":d.count(),"pageData":serializer.data,"totalPages":1,"currentPage":0}}, status=HTTP_200_OK)
except:
return JsonResponse({"message": "details not exist","error":True,"code":200,"results":{}}, status=HTTP_200_OK)

最新更新