所以我有这个序列化程序
class MovieSerializer(serializers.ModelSerializer):
class Meta:
model = Movie
fields = ('name', 'thumbnail', 'source_url', 'duration')
class SingleCategorySerializer(serializers.ModelSerializer):
movies = MovieSerializer(many=True, read_only=True)
class Meta:
model = MoviesCategories
fields = ('movies',)
电影模型与电影类别的关系如下
category = models.ForeignKey(MoviesCategories, related_name="movies", on_delete=models.DO_NOTHING)
现在当我尝试这个的时候
serializer = SingleCategorySerializer(movie_category, many=True)
,serializer.data
的结果是
[
{
"movies": [
{
"name": "Guardians of the Galaxy",
"thumbnail": "https://nerdist.com/wp-content/uploads/2016/08/Guardians-of-the-Galaxy-poster-2.jpg",
"source_url": "http://dl.tehmovies.org/94/10/Star.Wars/Star.Wars.Episode.II.Attack.of.the.Clones.2002.1080p.5.1CH.Tehmovies.ir.mkv",
"duration": "05:30:09"
},
{
"name": "Star Wars II",
"thumbnail": "https://images-na.ssl-images-amazon.com/images/I/51TEG6X589L.jpg",
"source_url": "http://dl.tehmovies.org/94/10/Star.Wars/Star.Wars.Episode.II.Attack.of.the.Clones.2002.1080p.5.1CH.Tehmovies.ir.mkv",
"duration": "05:32:26"
}
]
}
]
我只希望返回movies
的值。像这样
[
{
"name": "Guardians of the Galaxy",
"thumbnail": "https://nerdist.com/wp-content/uploads/2016/08/Guardians-of-the-Galaxy-poster-2.jpg",
"source_url": "http://dl.tehmovies.org/94/10/Star.Wars/Star.Wars.Episode.II.Attack.of.the.Clones.2002.1080p.5.1CH.Tehmovies.ir.mkv",
"duration": "05:30:09"
},
{
"name": "Star Wars II",
"thumbnail": "https://images-na.ssl-images-amazon.com/images/I/51TEG6X589L.jpg",
"source_url": "http://dl.tehmovies.org/94/10/Star.Wars/Star.Wars.Episode.II.Attack.of.the.Clones.2002.1080p.5.1CH.Tehmovies.ir.mkv",
"duration": "05:32:26"
}
]
任何帮助都将不胜感激。你知道吗
根据你的帖子,
serializer.data
返回使用简单的列表索引。
serializer.data
返回包含一个元素的列表。因此,为了得到这个结果,您可以调用serializer.data[0]
。那个元素是字典。所以要访问它,只需使用serializer.data[0]["movies"]
。上面写着,“嘿,python,我想要这个列表的第一个元素(序列化程序.data),从那个元素…哦,它是dict
。我可以要一个键为"movies"
的项目吗?”你知道吗相关问题 更多 >
编程相关推荐