创建一个模型,List应该返回他的所有电影,他工作的电影总数(django)



我的任务是**创建一个演员模型,List应该返回他所有的电影,他工作的电影总数

创建一个类型模型,List应该返回该类型下的所有电影,聚合电影总数

创建一个电影模型,该模型具有名称、演员和类型字段。这里演员和类型应该是多多多的关系**

我已经这样做了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)),
]

我不能做这个部分**列表应该返回他所有的电影,他工作的电影总数**

如果要获取相关字段,需要设置related_name字段

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

然后你需要在serialzers.py文件中添加RelatedField

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']

然后在JSON中,你可以得到他的电影,类型的电影等。希望能帮到你。

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']

现在可以显示电影中演员的数量

相关内容

  • 没有找到相关文章

最新更新