【问题标题】:How to include a field from the joining table in the queryset?如何在查询集中包含连接表中的字段?
【发布时间】:2017-07-31 17:56:09
【问题描述】:

我们正在使用 Django Rest Framework,但我是一名前端开发人员,他决定为他的下一个开源项目做一点 DRF。

我不想复制粘贴很多代码,因为您可以看到我所有的序列化程序、模型等...on GitHub

我已经询问了工作中的开发人员。有人说我应该覆盖to_representation,这似乎不对,因为我们正在从头开始构建序列化模型。而工作中的其他 Django 开发人员建议我查看custom relation fields

基本上给定以下序列化程序,我如何将连接表中的“类型”(命名错误)列包含在我的查询集中:

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'countries')

在伪代码中,我正在寻找类似以下的内容,因此当您获得用户时,您还会获得关联的countries,这通过多对多关系以及加入表中的“类型”:

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'countries', 'pinned.type')

我希望得到类似于以下内容的 JSON 响应:

{
    "username": "wildhoney",
    "countries": [
        { "country_id": 4, "type": 1 },
        { "country_id": 5, "type": 2 },
        { "country_id": 6, "type": 3 }
    ]
}

目前我只收到country_ids 的列表。

【问题讨论】:

    标签: django orm django-rest-framework


    【解决方案1】:

    您将要使用这样的嵌套关系:

    class TrackSerializer(serializers.ModelSerializer):
        class Meta:
            model = Track
            fields = ('order', 'title', 'duration')
    
    class AlbumSerializer(serializers.ModelSerializer):
        tracks = TrackSerializer(many=True, read_only=True)
    
        class Meta:
            model = Album
            fields = ('album_name', 'artist', 'tracks')
    

    来源:http://www.django-rest-framework.org/api-guide/relations/#nested-relationships

    所以你的情况是:

    class PinnedSerializer(ModelSerializer):
        class Meta:
            model = Pinned
            fields = ('type', 'country_id')
    
    class UserSerializer(ModelSerializer):
        pins = PinnedSerializer(many=True, read_only=True)
        class Meta:
            model = User
            fields = ('id', 'username', 'countries', 'pins')
    

    用实际项目测试的最新版本:

    class PinnedSerializer(serializers.ModelSerializer):
        country_id = serializers.ReadOnlyField(source='country.id')
        class Meta:
            model = Pinned
            fields = ('type', 'country_id')
    
    
    class UserSerializer(serializers.ModelSerializer):
        pinned_set = PinnedSerializer(many=True, read_only=True)
    
        class Meta:
            model = User
            fields = ('id', 'username', 'pinned_set')
    

    输出:

    {
        "id": 3,
        "username": "bobdole",
        "pinned_set": [
            {
                "type": 1,
                "country_id": 250
            }
        ]
    }
    

    【讨论】:

    • 我这样做并得到:字段名称type 对模型Country 无效。是因为typePinnedSerializer 上,而不是CountrySerializer 上吗?
    • 啊,对不起。我误解了。我以为typeCountry 上的一个字段。看起来您可能有点倒退,并且您希望在回复中使用pins 列表而不是countries
    • 也许,但我不知道如何实现。 Source code is on Github。对于/user/wildhoneyI receive thisI ideally want this
    • @Wildhoney 我意识到这不是最优的,但如果您愿意接受pinned_set 键下的数据,您可以使用我更新的答案中的代码。我已经在您的代码中对其进行了测试,并且可以正常工作(我相信)。
    • 太棒了,很高兴我能帮上忙。这对我来说也是一次学习经历:)
    猜你喜欢
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 2013-06-04
    • 2019-06-10
    • 1970-01-01
    • 2021-11-08
    相关资源
    最近更新 更多