views.py
这是我的models.py文件
models.py
from django.db import models
from django.utils import timezone
class Movielist(models.Model) :
Title = models.CharField(max_length=1000)
Description = models.TextField(blank=True)
ReleaseDate = models.DateTimeField(verbose_name='Release Date', blank=True)
Upvote = models.IntegerField(default=0)
Downvote = models.IntegerField(default=0)
def __str__(self):
return self.Title
class Actorlist(models.Model):
Name = models.CharField(max_length=1000)
DateofBirth = models.DateTimeField(verbose_name='Date of Birth',blank=True)
def __str__(self):
return self.Name
class ActorInMovie(models.Model):
Movie = models.ForeignKey(Movielist, default=1, on_delete=models.CASCADE, blank=True, related_name='ActorInMovie')
Actor = models.ForeignKey(Actorlist, default=1, on_delete=models.CASCADE, blank=True,related_name='ActorsMovie')
def __str__(self):
return self.Movie.Title
这是我的views.py文件views.py
@api_view(['GET'])
def apiOverview(request):
api_urls = {
'List':'/task-list/',
'Detail View':'/task-detail/<str:pk>/',
'Create':'/task-create/',
'Update':'/task-update/<str:pk>/',
'Delete':'/task-delete/<str:pk>/',
}
return Response(api_urls)
@api_view(['GET'])
def MovieName(request):
MovieName = Movielist.objects.prefetch_related('ActorInMovie')
serializer = MovieListSerializer(MovieName, many=True)
return Response(serializer.data)
这是我的serializer .py文件serializers.py
from rest_framework import serializers
from .models import *
class MovieListSerializer(serializers.ModelSerializer):
class Meta:
model = Movielist
fields = "__all__"
作为输出,我得到如下
[
{
"id": 1,
"Title": "KGF Chapter 1",
"Description": "This is very thrilling and suspenseful movie",
"ReleaseDate": "2022-04-18T16:22:03Z",
"Upvote": 5,
"Downvote": 2
},
{
"id": 2,
"Title": "RRR",
"Description": "This is a south Indian movie",
"ReleaseDate": "2022-04-19T06:46:54Z",
"Upvote": 2,
"Downvote": 3
},
{
"id": 3,
"Title": "Doctor Strange",
"Description": "this movie is a part of marvel cinematic universe.",
"ReleaseDate": "2022-04-22T07:19:07Z",
"Upvote": 12,
"Downvote": 7
}
]
谁能告诉我如何得到这样的输出
[
{
"id": 1,
"Title": "KGF Chapter 1",
"Description": "This is very thrilling and suspenseful movie",
"ReleaseDate": "2022-04-18T16:22:03Z",
"Actor": {yxz, yzx, xyz}
"Upvote": 5,
"Downvote": 2
},
{
"id": 2,
"Title": "RRR",
"Description": "This is a south Indian movie",
"ReleaseDate": "2022-04-19T06:46:54Z",
"Actor": {yxz, yzx, xyz}
"Upvote": 2,
"Downvote": 3
},
{
"id": 3,
"Title": "Doctor Strange",
"Description": "this movie is a part of marvel cinematic universe.",
"ReleaseDate": "2022-04-22T07:19:07Z",
"Actor": {yxz, yzx, xyz}
"Upvote": 12,
"Downvote": 7
}
]
请帮我解决这个错误。我必须进一步链接这个角,所以请帮我找到一个合适的输出
首先,我将在电影和演员之间使用多对多关系。它看起来像这样:
models.py:
class Actorlist(models.Model):
Name = models.CharField(max_length=1000)
DateofBirth = models.DateTimeField(verbose_name='Date of Birth',blank=True)
def __str__(self):
return self.Name
class Movielist(models.Model) :
Title = models.CharField(max_length=1000)
Description = models.TextField(blank=True)
ReleaseDate = models.DateTimeField(verbose_name='Release Date', blank=True)
Upvote = models.IntegerField(default=0)
Downvote = models.IntegerField(default=0)
Actors = models.ManyToManyField(Actorlist)
def __str__(self):
return self.Title
之后,您应该在输出中看到演员。我还建议阅读django-rest-framework中的关系文档:
https://www.django-rest-framework.org/api-guide/relations/