naive_user
naive_user

Reputation: 119

Get object as output instead of List in Django

I'm trying to get output result as an object but i'm getting result as a list.

My view:

def Expense_with_id(request, id):
    details = ExSerializer(Cat.objects.filter(id=id).all(), many=True).data
    return JsonResponse(details, safe=False)

Output:
    [{
      "id": 1,
      "category": 1,
       ...... 

     }]

I want my output to be :

Expected Output:

    {
      "id": 1,
      "category": 1,
       ...... 

    }

How can I achieve this with the current query.

Upvotes: 0

Views: 301

Answers (2)

Ryhan Ahmed
Ryhan Ahmed

Reputation: 73

use get() when you want to get a single unique object, and filter() when you want to get all objects that match your lookup parameters.

Upvotes: 0

sandeshdaundkar
sandeshdaundkar

Reputation: 903

ExSerializer(Cat.objects.get(id=id))

You are doing filter() instead of get().

Upvotes: 1

Related Questions