1

I am new to django rest framework, I am trying to build a simple movie API. I created 3 models Movie, Cast and Reviews, after applying migrations I added values in tables in django admin. When I start the server the api works fine until I again add values in Cast table or reviews table it gives me error like: "'Cast' object is not iterable". And its also not showing the Movie table data. What am I doing wrong. Here is My code:

models.py

from django.db import models

# Create your models here.


class Cast(models.Model):
    actor = models.CharField(max_length=225)
    producer = models.CharField(max_length=225)
    director = models.CharField(max_length=225)
    screenwriter = models.CharField(max_length=225)


class Reviews(models.Model):
    name = models.CharField(max_length=225)
    comment = models.CharField(max_length=225)


class Movies(models.Model):
    name = models.CharField(max_length=225)
    genre = models.CharField(max_length=225)
    ratings = models.IntegerField()
    cast = models.ForeignKey(Cast, on_delete=models.CASCADE)
    review = models.ForeignKey(Reviews, on_delete=models.CASCADE)

serializer.py

from .models import Movies, Reviews,Cast
from rest_framework import serializers


class CastSerializer(serializers.ModelSerializer):
    class Meta:
        model = Cast
        fields = ('actor', 'producer', 'director', 'screenwriter')


class ReviewsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Reviews
        fields = ('name', 'comment')


class MoviesSerializer(serializers.ModelSerializer):
    cast = CastSerializer(many=True)
    review = ReviewsSerializer(many=True)

    class Meta:
        model = Movies
        fields = ('name', 'genre',  'ratings', 'cast' , 'review')

views.py

from django.shortcuts import get_object_or_404
from .serializers import MoviesSerializer, ReviewsSerializer, CastSerializer
from rest_framework import viewsets
from rest_framework.response import Response
from .models import Movies,  Reviews, Cast
# Create your views here


class MovieViewSet(viewsets.ViewSet):
    def list(self, request):
        cast = Cast.objects.all()
        movies = Movies.objects.all()
        reviews = Reviews.objects.all()
        print(movies)
        movies_serializer = MoviesSerializer(movies, many=True)
        cast_serializer = CastSerializer(cast, many=True)
        reviews_serializer = ReviewsSerializer(reviews, many=True)
        data = {'movies_serializer': movies_serializer.data, 'cast_serializer': cast_serializer.data,
                'reviews_serializer': reviews_serializer.data}
        return Response(data)

urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('movies/', include('api.urls'))
]

1 Answer 1

3

Here the case and review are in a FK relation with the Movie and hence your MoviesSerializer class must looks like

class MoviesSerializer(serializers.ModelSerializer):
    cast = CastSerializer() # remove `many=True`
    review = ReviewsSerializer() # remove `many=True`

    class Meta:
        model = Movies
        fields = ('name', 'genre',  'ratings', 'cast' , 'review')
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it resolve the error, can you kindly explain why Many=true was giving error?
When you put many=True DRF expect the data to be iterable. In your case, Movies.cast is not an iterable.\

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.