【发布时间】:2018-07-01 17:37:53
【问题描述】:
所以我有这个序列化器
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"
}
]
任何帮助将不胜感激。
【问题讨论】:
-
迈克尔,这不是星球大战II而是克隆人的进攻
标签: python django python-3.x python-2.7 django-rest-framework