DRF序列化程序不在默认HTML表单页面API中显示字段(外键和many2many)。字段在GET

问题描述 投票:1回答:2

DRF序列化器包含一个组和清单字段,它们是许多和外键。默认DRF HTML表单中缺少它,但在GET视图中可用。当前,深度字段在序列化器中启用。如果我要删除深度,则外键可以使用默认的HTML格式,但是仍然缺少groupman2many组。我需要POST呼叫或DRF HTML表单中的两个字段。

我必须写一些创建方法,但是我不想为外键创建新记录,并且many2many只想利用现有字段。

我的序列化器类。

class MainHostSerializer(serializers.ModelSerializer):
    class Meta:
        model = MainHost
        fields = (
            'host_id', 
            'host_name',
            'inventory',
            'group'
        )  
        # depth = 2 

默认DRF HTML表单的原始视图

{
    "host_id": null,
    "host_name": ""
}

模型类

class MainHost(models.Model):
    host_id =  models.IntegerField(verbose_name='HOST ID', primary_key=True)
    host_name =  models.CharField(verbose_name='HOST NAME', max_length=512)
    inventory = models.ForeignKey(related_name='inv_ins', on_delete=models.SET_NULL, to='hosts.MainInventory', blank=True, null=True)
    group = models.ManyToManyField(MainGroup, related_name='hostgroups', through ='HostGroup')
python-3.x django-models django-rest-framework django-templates django-serializer
2个回答
0
投票

MainHost序列化程序的创建方法

def create(self, validated_data):
    inv_data = validated_data.pop('inventory')
    inv_res = MainInventory.objects.create(**inv_data)

    group_data = validated_data.pop('group')
    host_data = MainHost.objects.create(inventory = inv_res, **validated_data)

    for g_data in group_data:
        inv_data = g_data.pop('inv_id')
        inv = MainInventory.objects.create(**inv_data)
        group_res = MainGroup.objects.create(inv_id = inv, **g_data)
        print(validated_data)
        HostGroup.objects.create(host = host_data, group =  group_res)

0
投票

这是示例JSON

{
    "count": 1692,
    "next": "http://127.0.0.1:8000/api/mainhost/?page=2",
    "previous": null,
    "results": [
        {
            "host_id": 4087,
            "host_name": "10.240.144.2",
            "inventory": {
                "inv_id": 91,
                "inv_name": "GNS Switches (TestNet)",
                "total_hosts": 539,
                "total_groups": 1,
                "org_name": "ABC_TestNet",
                "description": "Inventory of ABC switches on Testnet",
                "inv_variables": "environ: testnet"
            },
            "group": [
                {
                    "group_id": 280,
                    "group_name": "aruba",
                    "total_hosts": 539,
                    "total_groups": 0,
                    "inv_id": {
                        "inv_id": 91,
                        "inv_name": "ABC Switches (TestNet)",
                        "total_hosts": 539,
                        "total_groups": 1,
                        "org_name": "ABC_TestNet",
                        "description": "Inventory of ABC switches on Testnet",
                        "inv_variables": "environ: testnet"
                    },
                    "description": "imported",
                    "group_variables": "{}",
                    "groupinv_name": "ABC Switches (TestNet)",
                    "groupinv_description": "",
                    "groupinv_source": "scm",
                    "groupinv_path": "TEC/GNS/Switches/testnet.ini"
                }
            ],
            "description": "imported",
            "foreman_group": "[{'id': 280, 'name': 'aruba'}]",
            "host_variables": "{}",
            "ansible_facts": "{}"
        }
    ]
}
© www.soinside.com 2019 - 2024. All rights reserved.