class Business(models.Model): caption = models.CharField(max_length=32) code = models.CharField(max_length=32, null=True) # fk = models.ForeignKey(to='Foo', on_delete=models.CASCADE) class Host(models.Model): nid = models.AutoField(primary_key=True) hostname = models.CharField(max_length=32, db_index=True) ip = models.GenericIPAddressField(db_index=True) port = models.IntegerField() b = models.ForeignKey(to='Business', on_delete=models.CASCADE, to_field='id')
由Host绑定Business
通过
b = models.ForeignKey(to='Business', on_delete=models.CASCADE, to_field='id')实现两个数据库的绑定
获取数据的3种方式
1.对象
v1 = models.Host.objects.filter(nid__gt=0)
2.字典
v2 = models.Host.objects.filter(nid__gt=0).values('nid', 'hostname', 'b_id', 'b__caption')
3.元组
v3 = models.Host.objects.filter(nid__gt=0).values_list('nid', 'hostname', 'b_id', 'b__caption')