【问题标题】:django serializer with nested column带有嵌套列的 django 序列化程序
【发布时间】:2014-09-08 16:46:03
【问题描述】:

我正在调用以下序列化程序 -

class ResourceSerializer(serializers.ModelSerializer):

    class Meta:
        model = Resmst
        resource_name = 'resmst'
        fields = ('resmst_id', 'resmst_name', 'resmst_desc', 'resmst_limit', 'resmst_inuse', 'resmst_active', 'resmst_lstchgtm', 
            'resmst_prntid', 'resmst_owner', 'resmst_public', 'resmst_locked', 'resmst_offline')
        read_only_fields = ('resmst_id',)

resmst_owner 是与另一个表的 FK 关系。我想要做的是让序列化程序显示与 FK 相关的列中的信息。

这是当前的 json -

[
    {
        "resmst_id": 204, 
        "resmst_name": "GAWK", 
        "resmst_desc": null, 
        "resmst_limit": 1, 
        "resmst_inuse": 0, 
        "resmst_active": "Y", 
        "resmst_lstchgtm": "2014-08-20T11:15:18", 
        "resmst_prntid": null, 
        "resmst_owner": 822, 
        "resmst_public": "Y", 
        "resmst_locked": null, 
        "resmst_offline": 0
    }
]

这就是我想要的样子 -

[
    {
        "resmst_id": 204, 
        "resmst_name": "GAWK", 
        "resmst_desc": null, 
        "resmst_limit": 1, 
        "resmst_inuse": 0, 
        "resmst_active": "Y", 
        "resmst_lstchgtm": "2014-08-20T11:15:18", 
        "resmst_prntid": null, 
        "owner_name": "John Smith", 
        "resmst_public": "Y", 
        "resmst_locked": null, 
        "resmst_offline": 0
    }
]

或者我是否坚持这样做 -

[
    {
        "resmst_id": 204, 
        "resmst_name": "GAWK", 
        "resmst_desc": null, 
        "resmst_limit": 1, 
        "resmst_inuse": 0, 
        "resmst_active": "Y", 
        "resmst_lstchgtm": "2014-08-20T11:15:18", 
        "resmst_prntid": null, 
        "resmst_owner": {
            "owner_name": "John Smith"
        }, 
        "resmst_public": "Y", 
        "resmst_locked": null, 
        "resmst_offline": 0
    }
]

【问题讨论】:

    标签: django django-rest-framework


    【解决方案1】:

    尚未对此进行测试,但它看起来像 this question。尝试使用SerializerMethodField。

    【讨论】:

    • 感谢您在@Steven 的帮助下为我指明了正确的方向。
    【解决方案2】:

    我认为你应该可以使用普通的Field(注意这是只读的)。

    class ResourceSerializer(serializers.ModelSerializer):
        owner_name = serializers.Field(source='resmst_owner.owner_name')
    
        class Meta:
            model = Resmst
            resource_name = 'resmst'
            fields = ('resmst_id', 'resmst_name', 'resmst_desc', 'resmst_limit', 
                'resmst_inuse', 'resmst_active', 'resmst_lstchgtm', 
                'resmst_prntid', 'resmst_owner', 'resmst_public', 
                'resmst_locked', 'resmst_offline', 'owner_name', )
            read_only_fields = ('resmst_id',)
    

    不要忘记将它添加到 Meta.fields(如上)。

    见Core arguments部分:

    来源

    将用于填充 场地。可能是只接受一个 self 参数的方法,例如 Field(source='get_absolute_url'),或者可以使用点符号来 遍历属性,如Field(source='user.email')。

    值source='*'有特殊含义,用于表示 整个对象应该被传递到该字段。这个可以 对于创建嵌套表示很有用。 (见实施 以 PaginationSerializer 类为例。)

    默认为字段名称。

    【讨论】:

    • 感谢第一个为我提供了解决方案,但这个给了我更优雅的“我正在寻找的东西”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    • 2013-04-12
    • 2016-03-01
    • 2020-03-21
    • 1970-01-01
    • 2015-12-16
    相关资源
    最近更新 更多