Get latest ID in Django returns object is not iterable

前端 未结 1 404
难免孤独
难免孤独 2021-01-28 16:32

I tried to get the latest id in Django but get the error.

def getlatestid(request):
    cot_info = COT.objects.latest(\'id\')
    return JsonResponse({\"data\":         


        
相关标签:
1条回答
  • 2021-01-28 16:53

    latest(...) returns an object, not a list. So, you can try like this to fix the error:

    def getlatestid(request):
        cot_info = COT.objects.values().latest('id')
        return JsonResponse({"data": cot_info})
    
    0 讨论(0)
提交回复
热议问题