'Create a model, List should return all the movies of him, Aggregate total movies he worked (django )
My task was **Create a actor model, List should return all the movies of him, Aggregate total movies he worked
Create a genre model, List should return all the movies under the genre, Aggregate total movies
Create a movie model, that have name, actors and genre fields. Here actors and genre should be a manytomany relationship **
I have done this models.py
from django.db import models
class Actor(models.Model):
actors = models.CharField(max_length=100)
def __str__(self):
return self.actors
class Genre(models.Model):
genre = models.CharField(max_length=100)
def __str__(self):
return self.genre
class Movie(models.Model):
name = models.CharField(max_length=100)
actors = models.ManyToManyField(Actor)
genre = models.ManyToManyField(Genre)
def __str__(self):
return self.name
serializer.py
from rest_framework import serializers
from .models import Movie,Genre,Actor
class ActorSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Actor
fields = ['url','actors']
class GenreSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Genre
fields = ['genre']
class MovieSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Movie
fields = ['url','name','actors','genre']
views.py
from django.shortcuts import render
from rest_framework import viewsets
# Create your views here.
from .serializers import MovieSerializer,ActorSerializer,GenreSerializer
from .models import Movie,Actor,Genre
from django.db.models import Count
class movie(viewsets.ModelViewSet):
queryset = Movie.objects.all()
serializer_class = MovieSerializer
class actor(viewsets.ModelViewSet):
queryset = Actor.objects.all()
serializer_class = ActorSerializer
class genre(viewsets.ModelViewSet):
queryset = Genre.objects.all()
serializer_class=GenreSerializer
urls.py
from django.urls import include, path
from rest_framework import routers
from .views import *
# define the router
router = routers.DefaultRouter()
# define the router path and viewset to be used
router.register(r'movie', movie)
router.register(r'actor', actor)
router.register(r'genre',genre)
urlpatterns = [
path('', include(router.urls)),
]
I cant able to do this part ** List should return all the movies of him, Aggregate total movies he worked **
Solution 1:[1]
You need to set the related_name field if you want to get the related fields.
class Movie(models.Model):
name = models.CharField(max_length=100)
actors = models.ManyToManyField(Actor, related_to = "actor_movies")
genre = models.ManyToManyField(Genre, related_to = "genre_movies")
def __str__(self):
return self.name
And then you need to add the RelatedField in serialzers.py file,
class ActorSerializer(serializers.HyperlinkedModelSerializer):
actor_movies = serializers.RelatedField(many = True, read_only = True)
class Meta:
model = Actor
fields = ['url','actors', 'actor_movies']
class GenreSerializer(serializers.HyperlinkedModelSerializer):
genre_movies = serializers.RelatedField(many = True, read_only = True)
class Meta:
model = Genre
fields = ['genre', 'genre_movies']
Then in JSON, you can get the movies of him, movies of the genre, etc. Hope it could help.
Solution 2:[2]
class ActorSerializer(serializers.HyperlinkedModelSerializer):
count_actor_movies = serializers.SerializerMethodField()
def get_count_actor_movies(self, instance):
return instance.actor_movies.count()
class Meta:
model = Actor
fields = ['id','actors','count_actor_movies']
now it work fine it show the count of actors in the movie
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | David Lu |
| Solution 2 | Abilash K.G |
