我有两张表,Instances和DataDisk,如下:
class Instances(models.Model):
instance_id = models.CharField("服务器ID", unique=True, max_length=100, help_text="服务器ID")
# project_id = models.ForeignKey("项目名称", Projects, to_fields='project_id')
project = models.ForeignKey(Projects, max_length=100, help_text="项目")
application = models.CharField("应用名称", max_length=10, help_text="应用名称")
system = models.CharField("操作系统", max_length=100, help_text="操作系统")
cpu_info = models.CharField("CPU信息", max_length=100, help_text="CPU信息")
mem_info = models.CharField("内存信息", max_length=100, help_text="内存信息")
disk_size = models.CharField("系统盘大小", max_length=100, help_text="系统盘大小")
disk_type = models.CharField("系统盘类型", max_length=30, help_text="系统盘类型")
disk_id = models.CharField("系统盘ID", max_length=50, help_text="系统盘ID")
wip = models.GenericIPAddressField("外网IP", null=True, blank=True, help_text="外网IP")
nip = models.GenericIPAddressField("内网IP", null=True, blank=True, help_text="内网IP")
vpc_id = models.CharField("vpcid",null=True, blank=True, max_length=100, help_text="vpcid")
subnet_id = models.CharField("子网ID", null=True, blank=True, max_length=100, help_text="子网ID")
cidr = models.CharField("子网网段", null=True, blank=True, max_length=100, help_text="子网网段")
region = models.CharField("区域", max_length=30, help_text="区域")
zone = models.CharField("可用区域", max_length=30, help_text="可用区域")
status = models.CharField("状态", max_length=10, help_text="状态")
create_time = models.DateTimeField("创建日期", help_text="创建日期")
update_time = models.DateTimeField("更新日期", auto_now=True)
def __str__(self):
return '{project}-{instanceid}-'.format(project=self.project.name, instanceid=self.instance_id)
class Meta:
db_table = "instances"
class DataDisk(models.Model):
disk_type = models.CharField("数据盘类型", max_length=20, help_text="数据盘类型")
disk_id = models.CharField("数据盘ID", max_length=50, help_text="数据盘ID")
disk_size = models.CharField("数据盘大小", max_length=50, help_text="数据盘大小")
host = models.ForeignKey(Instances, help_text="主机", on_delete=models.CASCADE)
def __str__(self):
return '{host}-{disk_id}-'.format(host=self.host.application, disk_id=self.disk_id)
class Meta:
db_table = "datadisks"
serializers文件内容如下:
class InstancesSerializer(serializers.ModelSerializer):
"""
服务器序列化类
"""
# data_disk = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
class Meta:
model = Instances
fields = "__all__"
class DatadiskSerializer(serializers.ModelSerializer):
"""
数据盘序列化类
"""
class Meta:
model = DataDisk
fields = "__all__"
views内容如下:
class InstancesViewset(viewsets.ReadOnlyModelViewSet):
"""
list:
列出所有服务器信息
retrieve:
读取一个服务器信息
"""
queryset = Instances.objects.all()
serializer_class = InstancesSerializer
class DatadiskViewset(viewsets.ReadOnlyModelViewSet):
"""
list:
列出所有硬盘信息
retrieve:
读取一个硬盘信息
"""
queryset = DataDisk.objects.all()
serializer_class = DatadiskSerializer
我如何访问服务器信息时,把该服务器所关联的硬盘也显示出来?
貌似不行,
结果:
还是没有硬盘信息