- 论坛徽章:
- 0
|
本帖最后由 zhujinzz 于 2011-08-25 10:32 编辑
如题
a,b,c,d四个表,a中存放e信息(比如主机名),b中存放f信息(比如服务web),
c中存放g信息(比如IP),a,b,c值唯一
d分别外键a,b,c
现在要以字典形式返回信息:- info = {'host1':(ip,web),'host2':(ip,web)}
复制代码 在django中做外键查询,怎么堆代码?或者有其他方式,返回信息,主机名对应ip和服务信息?
路过的大虾猛踩吧,不要留情,谢谢!
问题解决,感谢老王(http://www.cnpythoner.com/post/137.html),谢谢大家,收贴!- mysql> select * from djt_a;
- +----+-------+
- | id | e |
- +----+-------+
- | 1 | test1 |
- +----+-------+
- 1 row in set (0.00 sec)
- mysql> select * from djt_b;
- +----+--------+
- | id | f |
- +----+--------+
- | 1 | apache |
- +----+--------+
- 1 row in set (0.00 sec)
- mysql> select * from djt_c;
- +----+--------------+
- | id | g |
- +----+--------------+
- | 1 | 192.168.1.10 |
- +----+--------------+
- 1 row in set (0.00 sec)
- mysql> select * from djt_d;
- +----+---------+------------+-------+
- | id | host_id | service_id | ip_id |
- +----+---------+------------+-------+
- | 1 | 1 | 1 | 1 |
- +----+---------+------------+-------+
- 1 row in set (0.00 sec)
复制代码- In [1]: from djtest.djt import models
- In [2]: t = models.d.objects.select_related().get(id=1)
- In [3]: t
- Out[3]: <d: d object>
- In [5]: t.host.e
- Out[5]: u'test1'
- In [7]: t.service.f
- Out[7]: u'apache'
- In [8]: t.ip.g
- Out[8]: u'192.168.1.10'
复制代码 |
|