如何将冗余对象序列化为序列化程序?

时间:2017-11-14 04:00:32

标签: python django django-rest-framework

我有一个型号名称CloudServer,它有一个openstackserverid字段,通过它我可以得到一个对象:

class CloudServer(models.Model):
    openstackserverid = models.CharField(max_length=128)   
    buytime = models.ForeignKey(to=BuyTime)   
    expiration_time = models.DateTimeField()  
    availablearea = models.ForeignKey(to=AvailableArea)   
    profile = models.TextField()   

在序列化程序中:

class CloudServerListSerializer(ModelSerializer):
    class Meta:
        model = CloudServer
        fields = "__all__"

在视图中:

class CloudServerListAPIView(ListAPIView):
    serializer_class = CloudServerListSerializer

我可以使用bellow函数获取openstackserver对象。

def getOpenstackServer(openstackserverid):

    ...
    return openstackserver  

openstackserver对象如下:

openstack.compute.v2.server.ServerDetail(OS-EXT-STS:task_state=None, 
addresses={u'private': [{u'OS-EXT-IPS-MAC:mac_addr': u'fa:16:3e:23:75:7b', u'version': 4, u'addr': u'192.168.1.8', u'OS-EXT-IPS:type': u'fixed'}]}, 
links=[{u'href': u'http://controller:8774/v2.1/99a50773b170406b8902227118bb72bf/servers/0672107f-e580-4a24-9d0a-867831d6b7d2', u'rel': u'self'}, {u'href': u'http://controller:8774/99a50773b170406b8902227118bb72bf/servers/0672107f-e580-4a24-9d0a-867831d6b7d2', u'rel': u'bookmark'}], 
image={u'id': u'ecbd1ef0-7dcf-41ff-8618-4501aa4e3945', 
u'links': [{u'href': u'http://controller:8774/99a50773b170406b8902227118bb72bf/images/ecbd1ef0-7dcf-41ff-8618-4501aa4e3945', u'rel': u'bookmark'}]}, 
OS-EXT-STS:vm_state=active, OS-EXT-SRV-ATTR:instance_name=instance-00000047, OS-SRV-USG:launched_at=2017-11-10T12:52:09.000000, flavor={u'id': u'a27c55bc-ef20-4dcf-a924-a02a4361fc01', 
u'links': [{u'href': u'http://controller:8774/99a50773b170406b8902227118bb72bf/flavors/a27c55bc-ef20-4dcf-a924-a02a4361fc01', u'rel': u'bookmark'}]}, id=0672107f-e580-4a24-9d0a-867831d6b7d2, 
security_groups=[{u'name': u'default'}], user_id=fb52853bde3d4d3e8e831749781f8671, OS-DCF:diskConfig=MANUAL, accessIPv4=, accessIPv6=, progress=0, OS-EXT-STS:power_state=1, OS-EXT-AZ:availability_zone=nova, metadata={}, status=ACTIVE, updated=2017-11-10T12:52:09Z, hostId=76f61a58cf6d411a30e3e34da4dd252a03aa0093d9dd19c653b234b3, OS-SRV-USG:terminated_at=None, key_name=None, OS-EXT-SRV-ATTR:hypervisor_hostname=controller, name=vm-00000, created=2017-11-10T12:51:44Z, tenant_id=99a50773b170406b8902227118bb72bf, os-extended-volumes:volumes_attached=[], config_drive=)

到目前为止,据我所知,我只能序列化CloudServer模型的字段。

我可以在序列化器或视图中序列化对象吗?以及如何处理?

1 个答案:

答案 0 :(得分:0)

如果我理解正确,openstackserver不是Django模型。 您可以在序列化程序中添加method field

首先,您需要为openstackserver个对象定义一个序列化程序:

class OpenStackServerSerializer(serializers.Serializer):
    """Serializer for the `openstackserver` object"""

    addresses = serializers.DictField()
    # Other fields of your `openstackserver` object 

    class Meta:
        fields = (
            'adresses',
            # Other fields of your `openstackserver` object 
        )

class CloudServerListSerializer(serializers.ModelSerializer):
     """Serializer for the `CloudServer` object"""

     openstackserver = serializers.SerializerMethodField()

     class Meta:
          fields = (
              'openstackserver',
              # Other fields...
          )

     def get_openstackserver(self, cloud_server):
         """Get the openstack from the current `CloudServer` instance and serialize it"""

         openstackserver = getOpenstackServer(cloud_server.openstackserverid)
         return OpenStackServerSerializer(openstackserver).data

像这样,您应该拥有与您的云服务器相关联的每个openstackserver