简体   繁体   English

Django REST Serializer 多对多字段仅通过表 id 显示,需要多条记录列表

[英]Django REST Serializer Many to Many field displays only through table id, need list of many records

How do i get list of Many 2 Many field objects?如何获取 Many 2 Many 字段对象的列表? I am just getting the ID.我刚拿到身份证。

This one is MainHost class which is having Many2Many field这是具有 Many2Many 字段的 MainHost 类

class MainHost(models.Model):
    host_id =  models.IntegerField(verbose_name='HOST ID', primary_key=True)
    group = models.ManyToManyField(MainGroup, related_name='hostgroups', through ='HostGroup')

This class is created for through keyword, contains all the relationship for Host and Group这个类是为 through 关键字创建的,包含了 Host 和 Group 的所有关系

class HostGroup(models.Model):
    host = models.ForeignKey(MainHost, on_delete=models.CASCADE)
    group = models.ForeignKey(MainGroup, on_delete=models.CASCADE)

This class contains all the Groups data此类包含所有组数据

class MainGroup(models.Model):
    group_id =  models.IntegerField(verbose_name='GROUP ID', primary_key=True)

Serializer class序列化器类

class MainGroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = MainGroup
        fields = (
            'group_id', 
            'group_name',
             ....
            'groupinv_path'
        ) 

HostSerialzer class HostSerialzer 类

class MainHostSerializer(serializers.ModelSerializer):
    group = serializers.PrimaryKeyRelatedField(queryset=HostGroup.objects.all(), many=True)
    class Meta:
        model = MainHost
        fields = (
            'host_id', 
            'host_name',
            'group'
        )

class HostGroupSerializer(serializers.ModelSerializer):
    host = MainHostSerializer()
    group = MainGroupSerializer()
    class Meta:
        model = HostGroup
        fields = (
            'id',
            'host', 
            'group'  
        )  
        read_only_fields = ['host', 'group']
        def create(self, validated_data):
            host_data = MainHost.objects.create(**validated_data.get('host'))
            group_data = MainGroup.objects.create(**validated_data.get('group'))

            conn = HostGroup.objects.create(
                host=host_data, 
                group=group_data
            )
            return conn  

MainHost API response MainHost API 响应

    {
        "host_id": 4087,
        "host_name": "10.240.144.2",
        "group": [
            280,
            285,
            300
        ]
    }

280, 285 and 300 are group objects from MainGroup I want to see the whole object content. 280, 285 和 300 是来自 MainGroup 的组对象,我想查看整个对象内容。

MainGroup Response is showing the complete object. MainGroup Response 显示完整的对象。

    {
        "group_id": 2,
        "group_name": "test_environment_production",
        "total_hosts": 1,
        "total_groups": 0,
        "description": "imported",
        "group_variables": "{}",
        "groupinv_path": ""
    }

Even Response for GroupHost API is having complete Group Object.甚至对 GroupHost API 的响应也具有完整的组对象。

    {
        "id": 1,
        "host": {
            "host_id": 4087,
            "host_name": "10.100.144.2",
            "group": [
                280
            ]
        },
        "group": {
            "group_id": 280,
            "group_name": "aruba",
            "total_hosts": 539,
             .....
            "groupinv_path": "TEC/GT/Swi/test.ini"
        }
    }

Can specify the nested representation by using the depth option as shown in the DRF docs here:可以使用深度选项指定嵌套表示,如 DRF 文档中所示:

https://www.django-rest-framework.org/api-guide/serializers/#specifying-nested-serialization https://www.django-rest-framework.org/api-guide/serializers/#specifying-nested-serialization

Removing this line: group = serializers.PrimaryKeyRelatedField(queryset=HostGroup.objects.all(), many=True)删除这一行: group = serializers.PrimaryKeyRelatedField(queryset=HostGroup.objects.all(), many=True)

from MainHostSerializer(serializers.ModelSerializer):来自 MainHostSerializer(serializers.ModelSerializer):

and adding depth = 2 it worked fine并添加深度 = 2 效果很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 django在序列化器中从多到多字段中仅选择特定字段 - django select only specific fields from many to many field in serializer REST 中的多对多字段和嵌套序列化程序:覆盖嵌套序列化程序不会在 Django 中创建嵌套对象 - Many to Many field and Nested Serializer in REST: Overwriting Nested serializer doesn't create nested object in Django 在Django Rest框架中具有多对多关系的ID列表的请求 - request with list of id's for many to many relationship in django rest framework Django rest framewor处理许多= True序列化器数据以列出 - Django rest framewor deal with many=True serializer data to list 如何在 Django REST Framework 序列化程序中检索具有向后关系查找的多对多字段? - How to retrieve a many-to-many field with backward relationships lookup in Django REST Framework serializer? Django Rest通过id更新了许多对 - Django Rest update many to many by id Django 多 2 多通过 REST 框架 - Django many 2 many through rest framework Django:通过具有(仅)复合键的表的多对多 - Django: Many-to-many through a table with (only) compound key Django Rest Framework过滤多对多字段 - Django Rest Framework filtering a many to many field Django Rest Framework可以过滤多个领域 - Django Rest Framework filterset on many to many field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM