ORM 表结构;和管理员是多对多, 和 OS 是一对多

class hosts(models.Model):
out_ip=models.CharField(max_length=32)
in_ip=models.CharField(max_length=32)
app=models.ForeignKey(App)
login_user=models.CharField(max_length=32)
login_pwd=models.CharField(max_length=32)
os=models.ForeignKey(Os)
host_class=models.ForeignKey(Host_Class)
# admins=models.ForeignKey(Admins)
admins = models.ManyToManyField(Admins)
disk=models.IntegerField()
network=models.IntegerField()
desc=models.CharField(max_length=100,null=True)

class Os(models.Model):
name=models.CharField(max_length=32)
#所属管理员
class Admins(models.Model):
name=models.CharField(max_length=32)
def __str__(self):
return self.name

 

一对多:

Python中查询:

all_data=hosts.objects.all()
for i in all_data:
print(i.os.name)  -->查询出主机所对应的 系统名称

再Tamplate中
all_data=hosts.objects.all()
return render(req,'hosts_index.html',locals())

{% for obj in all_data %}
    <div class="c7 ca jg">{{ obj.os.name }}</div>
{% endfor %}



===========多对多:
Python中查询:
all_data=hosts.objects.all()
for i in all_data:
for p in i.admins.all():
print(p)
再Tamplate中
all_data=hosts.objects.all()
return render(req,'hosts_index.html',locals())


{% for obj in all_data %}
<div class="c4 ca jg">
     {% for i in obj.admins.all %}
{{ i }}
{% endfor %}
</div>
{% endfor %}