简体   繁体   English

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

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

i am using sqlite3 database in my python django application: i am defining my table structure which is similar to my actual table schema. 我在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

and so on .. 等等 ..

i want to access all table data with in single query in django on the basis of forgein key. 我想在forgein键的基础上在django中使用单个查询访问所有表数据。 its not necessary all tables having same no of record. 不必所有具有相同记录数的表。

i have already set foreign key in all model. 我已经在所有模型中设置了外键。 like : 喜欢 :

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

Use select_related() 使用select_related()

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

The select_related gets translated to INNER JOIN query and loads all the data immediately. 将select_related转换为INNER JOIN查询并立即加载所有数据。

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

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