0

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.

3
  • remove many=True Commented May 26, 2020 at 6:30
  • I tried but it did not work @ruddra Commented May 26, 2020 at 6:31
  • Try using get instead of filter then remove many=True Commented May 26, 2020 at 6:33

2 Answers 2

1

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

I am getting an error ->` object is not iterable` if I remove all and
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.