如何仅在Django-Rest-Framework中深入一个字段?

时间:2018-08-27 03:17:27

标签: python django django-rest-framework

我有一个IPv4ListSerializer,它有一个depth = 1

class IPv4ListSerializer(ModelSerializer):
    """
    ipv4
    """
    ip_status = serializers.CharField(read_only=True)
    class Meta:
        model = IPv4Manage
        fields = "__all__"
        depth = 1

但结果却变成了每个字段depth 1

[
    {
        "id": 281,
        "ip_status": "Vlaned",
        "ip": "43.243.33.1",
        "netmask": "255.255.255.248",
        "prefix": 29,
        "is_gateway": false,
        "is_network_ip": false,
        "is_broadcast_ip": false,
        "desc": null,
        "ctime": "2018-04-26T21:17:33.623092+08:00",
        "uptime": "2018-06-02T12:48:41.882016+08:00",
        "ipv4network": {
            "id": 21,
            "network_ip": "43.243.33.0",
            "prefix": 24,
            "ctime": "2018-04-26T21:17:33.533667+08:00",
            "uptime": "2018-04-26T21:17:33.533814+08:00",
            "area_partition": 7
        },
        "vlaned_ipv4network": {
            "id": 1,
            "network_ip": "43.243.33.0",
            "prefix": 29,
            "gateway_ip": "43.243.33.6",
            "broadcast_ip": "43.243.33.7",
            "ctime": "2018-04-26T21:27:56.011624+08:00",
            "uptime": "2018-04-26T21:27:56.258053+08:00",
            "ipv4network": 21,
            "vlanedipv4networkgroup": 4
        },
        "physical_server": {
            "id": 23,
            "name": "SE01-A1",
            "desc": "desc",
            "cpu": "Intel Xeon E3-1230 v5",
            "ram": "Supermicro  DDR4___16",
            "disk": "Inter SSD___240",
            "price": "1000.00",
            "server_status": "ready",
            "pay_time": null,
            "expire_time": null,
            "ipmi_addr": "172.16.30.1",
            "ipmi_account_and_pwd": null,
            "has_intranet": true,
            "intranet_ip": null,
            "customer_desc": null,
            "whmcs_tblorders_id": null,
            "ctime": "2018-05-08T16:15:50.639962+08:00",
            "uptime": "2018-08-20T18:25:27.289100+08:00",
            "switches": 3,
            "physical_server_model": 5,
            "switchesport": 57,
            "cabinet": 3,
            "task": null,
            "user": null,
            "trade_record": null
        }
    },
    ....
]

我只能深入一个领域吗?例如depth ipv4network,我的要求是:

[
    {
        "id": 281,
        "ip_status": "Vlaned",
        "ip": "43.243.33.1",
        "netmask": "255.255.255.248",
        "prefix": 29,
        "is_gateway": false,
        "is_network_ip": false,
        "is_broadcast_ip": false,
        "desc": null,
        "ctime": "2018-04-26T21:17:33.623092+08:00",
        "uptime": "2018-06-02T12:48:41.882016+08:00",
        "ipv4network": {
            "id": 21,
            "network_ip": "43.243.33.0",
            "prefix": 24,
            "ctime": "2018-04-26T21:17:33.533667+08:00",
            "uptime": "2018-04-26T21:17:33.533814+08:00",
            "area_partition": 7
        },
        "vlaned_ipv4network": 1,
        "physical_server": 23,
    },
    ....
]

1 个答案:

答案 0 :(得分:2)

depth 适用于特定型号的所有FK关系。因此,如果您需要删除某些特定字段的 深度功能 ,请对每个fk字段使用不同的序列化程序(种类为nested serializer

示例

class IPv4ListSerializer(ModelSerializer):
    ip_status = serializers.CharField(read_only=True)
    ipv4network = IPV4NetworkSerializer()

    class Meta:
        model = IPv4Manage
        fields = "__all__"
        depth = 1  # remove this "depth"