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.

Related :   How to define different urlpatterns in view.py for Django

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.


lynxbee_ezoic

Subscribe with Valid Email Id to receive updates in Inbox. ( Secured by Google FeedBurner )

   


Leave a Comment

Android Android Applications Android Build system Android Commands Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development & Build Development, Debugging and Performance Tools Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web design and development Website Hosting Wordpress Yocto / Bitbake / Openembedded