Mahdi
Mahdi

Reputation: 1035

Get latest ID in Django returns object is not iterable

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": list(cot_info)})

TypeError: 'COT' object is not iterable

Upvotes: 0

Views: 68

Answers (1)

ruddra
ruddra

Reputation: 51968

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})

Upvotes: 2

Related Questions