Home > Enterprise >  i am creating an API and i am getting the error 'ModelBase' object is not iterable error
i am creating an API and i am getting the error 'ModelBase' object is not iterable error

Time:09-08

this is my views.py

@api_view(['GET'])
def home(request):
    if request.method == 'GET':
        movies=Watchlist.objects.all()
        serializer=WatchlistSerializer(Watchlist,many=True)
        return Response(serializer.data)

this is serializers.py

class WatchlistSerializer(serializers.ModelSerializer):
    class Meta:
        model=Watchlist
        fields="__all__"

this is models.py

class Watchlist(models.Model):
    title=models.CharField(max_length=50)
    storyline=models.CharField(max_length=200)
    activate=models.BooleanField(default=True)
    create=models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

it is showing error model class not iterable.

CodePudding user response:

observe the lines below

movies=Watchlist.objects.all()
serializer=WatchlistSerializer(Watchlist,many=True)

the QuerySet is captured in movies not in Watchlist

it should be like this serializer=WatchlistSerializer(movies,many=True)

  • Related