外键约束示例
#models操作如下 class HostInfo(models.Model): servername = models.CharField(max_length=32) serverip = models.CharField(max_length=32) serverpassword = models.CharField(max_length=64) server_group = models.ForeignKey("GroupInfo",to_field="id") class GroupInfo(models.Model): caption = models.CharField(max_length=32)
注意: server_group 是一个对象 里边封装了id和caption,如果想取 caption可以通过如下方法:
for row in list
row.servername
row.serverip
row.server_group.caption
时间: 2024-12-23 00:28:35