【问题标题】:Returning values of a 'ReturnList' without the key in DjangoRestFramework在 Django Rest Framework 中返回没有键的“返回列表”的值
【发布时间】: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


【解决方案1】:

根据你的帖子,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"
         }
    ]
}
]

使用简单的列表索引。 serializer.data 返回一个包含一个元素的列表。所以要得到它,你会打电话给serializer.data[0]。该元素是字典。所以要访问它,只需使用serializer.data[0]["movies"]。这说,“嘿,python,我想要这个列表的第一个元素(serializer.data),然后从那个元素中......哦,它是dict。我可以拥有带有"movies" 键的项目吗? "

【讨论】:

  • 非常感谢。我也试过了,只是忘了把它放在代码print("Got a response ", serializer.data[0]['movies']) return Response(serializer.data)
  • 没问题,接受答案这样我就知道这个问题做完了
  • 剩余 52 秒 (:
猜你喜欢
  • 2021-10-02
  • 2016-07-03
  • 2013-04-09
  • 2019-08-19
  • 1970-01-01
  • 2022-12-23
  • 1970-01-01
  • 1970-01-01
  • 2016-05-30
相关资源
最近更新 更多