Home » Django & REST Api » Solved: MultipleObjectsReturned: get() returned more than one UserInfo — it returned

Solved: MultipleObjectsReturned: get() returned more than one UserInfo — it returned

While working with our another post “Developing REST API using functions in DRF application views” , we were trying to have one “user_id” for multiple json objects, so we could get only different values like name, age etc for the same user_id” field, but when we did http POST with same user_id for 2 json objects , we got following error after trying to retrieve the JSON using http GET,

helloproject.helloapp.models.UserInfo.MultipleObjectsReturned: get() returned more than one UserInfo -- it returned 2!

When we looked into views.py code, we were accessing the JSON objects as below,

user = UserInfo.objects.get(userid=pk)

so, this code will just fetch only one json object with userid as we pass to it, hence we got errors when there was two json objects with same user id.

Solution :

Change the code to use filters so it can fetch multiple json objects,

users = UserInfo.objects.filter(userid=pk)

Now, when you do http GET, you will not see this error.


Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment