簡體   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