Home » Django & REST Api » Solved: TypeError: object is not iterable

Solved: TypeError: object is not iterable

When we were writing the filters for the Django REST framework to get JSON objects based on some parameter we pass from url, we got an error as below,

TypeError: 'UserInfo' object is not iterable

here, UserInfo is our modal name from django, it can be any as per your code.

Solution :

Change the respective code from view.py

change “get” to “filter” from “UserInfo.objects.get” to “UserInfo.objects.filter”, i.e. in your respective view get the individual object using “filter” instead of “get”

class userInfo_by_primarykey(APIView):
    def get(self, request, primary_key, format=None):
        user = UserInfo.objects.get(pk=primary_key)

to

class userInfo_by_primarykey(APIView):
    def get(self, request, primary_key, format=None):
        user = UserInfo.objects.filter(pk=primary_key)

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

Leave a Comment