繁体   English   中英

多个表基于外键在Django中的单个查询集中?

[英]multiple tables join on basis of foreign key in single query set in django?

我在python django应用程序中使用sqlite3数据库:我定义的表结构与我的实际表架构相似。

table1 having three column:
1. id | primary key | int
2. name | text
3. address | text

table2 having four column:
1. id | primary key | int
2. name_id | foreign key | int 
3. name_info | text
4. address_info | text


table3 having four column:
1. id | primary key | int
2. name_id | foreign key | int 
3. edu_info | text
4. busin_info | text


table4 having four column:
1. id | primary key | int
2. name_id | foreign key | int 
3. progress_info | text
4. inventory_info | text

等等 ..

我想在forgein键的基础上在django中使用单个查询访问所有表数据。 不必所有具有相同记录数的表。

我已经在所有模型中设置了外键。 喜欢 :

class table(models.Model):
    name = models.ForeignKey(modelname)

使用select_related()

YourModel.objects.select_related().filter(foreign_record__foreign_attribute__gt=foo)

将select_related转换为INNER JOIN查询并立即加载所有数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM