溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Django框架多表查詢實(shí)例分析

發(fā)布時(shí)間:2020-08-23 11:27:11 來(lái)源:腳本之家 閱讀:159 作者:-Finley- 欄目:開發(fā)技術(shù)

本文實(shí)例講述了Django框架多表查詢。分享給大家供大家參考,具體如下:

多表查詢是模型層的重要功能之一, Django提供了一套基于關(guān)聯(lián)字段獨(dú)特的解決方案.

ForeignKey

來(lái)自Django官方文檔的模型示例:

from django.db import models
class Blog(models.Model):
  name = models.CharField(max_length=100)
  tagline = models.TextField()
class Author(models.Model):
  name = models.CharField(max_length=50)
  email = models.EmailField()
class Entry(models.Model):
  blog = models.ForeignKey(Blog)
  authors = models.ManyToManyField(Author)
  headline = models.CharField(max_length=255)
  body_text = models.TextField()
  pub_date = models.DateField()
  mod_date = models.DateField()
  n_comments = models.IntegerField()
  n_pingbacks = models.IntegerField()
  rating = models.IntegerField()

class ForeignKey

ForeignKey字段接受一個(gè)Model類作為參數(shù), 類型與被參照的字段完全相同:

blog = models.ForeignKey(Blog)

ForeignKey.to_field

關(guān)聯(lián)到的關(guān)聯(lián)對(duì)象的字段名稱。默認(rèn)地,Django 使用關(guān)聯(lián)對(duì)象的主鍵。

blog = models.ForeignKey(Blog, to_field=Blog.name)

ForeignKey.db_constraint

Django Model的ForeignKey字段的主要功能是維護(hù)一個(gè)一對(duì)多的關(guān)系, 以進(jìn)行關(guān)聯(lián)查詢.

只有在db_constraint=True時(shí)Django model才會(huì)在數(shù)據(jù)庫(kù)上建立外鍵約束, 在該值為False時(shí)不建立約束.

默認(rèn)db_constraint=True.

ForeignKey.related_name

這個(gè)名稱用于讓關(guān)聯(lián)的對(duì)象反查到源對(duì)象.

如果你不想讓Django 創(chuàng)建一個(gè)反向關(guān)聯(lián),請(qǐng)?jiān)O(shè)置related_name 為 '+' 或者以'+' 結(jié)尾.

ForeignKey.related_query_nameForeignKey.related_name作為默認(rèn)值, 兩者功能的具體說(shuō)明請(qǐng)參見相關(guān)文檔

使用ForeignKey查詢

前向查詢

若關(guān)系模型A包含與模型B關(guān)聯(lián)的關(guān)聯(lián)字段, 模型A的實(shí)例可以通過(guò)關(guān)聯(lián)字段訪問(wèn)與其關(guān)聯(lián)的模型B的實(shí)例:

>>> e = Entry.objects.get(id=2)
>>> e.blog # Returns the related Blog object.

修改e.blog并調(diào)用save方法存入數(shù)據(jù)庫(kù)

>>> e.blog = some_blog
>>> e.save()

如果ForeignKey 字段有null=True 設(shè)置(即它允許NULL值),可以分配None來(lái)刪除對(duì)應(yīng)的關(guān)聯(lián)性

>>> e = Entry.objects.get(id=2)
>>> e.blog = None
>>> e.save() # "UPDATE blog_entry SET blog_id = NULL ...;"

Django提供了一種使用雙下劃線__的查詢語(yǔ)法:

>>> Entry.objects.filter(blog__name='Beatles Blog')

反向查詢

被索引的關(guān)系模型可以訪問(wèn)所有參照它的模型的實(shí)例,如Entry.blog作為Blog的外鍵,默認(rèn)情況下Blog.entry_set是包含所有參照Blog的Entry示例的查詢集,可以使用查詢集API取出相應(yīng)的實(shí)例。

>>>b = Blog.objects.get(id=1)
>>>b.entry_set.all()

Entry.blog的related_name和related_query_name可以設(shè)置該查詢集的名字。

ManyToManyField

來(lái)自Django官網(wǎng)的示例:

from django.db import models
class Person(models.Model):
  name = models.CharField(max_length=50)
class Group(models.Model):
  name = models.CharField(max_length=128)
  members = models.ManyToManyField(Person, through='Membership', through_fields=('group', 'person'))
class Membership(models.Model):
  group = models.ForeignKey(Group)
  person = models.ForeignKey(Person)
  inviter = models.ForeignKey(Person, related_name="membership_invites")
  invite_reason = models.CharField(max_length=64)

class ManyToManyField

ManyToManyField.through

Django 會(huì)自動(dòng)創(chuàng)建一個(gè)表來(lái)管理多對(duì)多關(guān)系, 若要手動(dòng)指定關(guān)聯(lián)表則需要使用through關(guān)鍵字參數(shù).

ManyToManyField.through_fields

上文示例中Membership 有兩個(gè)外鍵指向Person (person 和inviter),這使得關(guān)聯(lián)關(guān)系含混不清并讓Django 不知道使用哪一個(gè)。

在這種情況下,必須使用through_fields 明確指定Django 應(yīng)該使用哪些外鍵

through_fields 接收一個(gè)二元組('field1', 'field2'),其中field1 為指向定義ManyToManyField 字段的模型的外鍵名稱(本例中為group),field2 為指向目標(biāo)模型的外鍵的名稱(本例中為person).

ManyToManyField.db_table

默認(rèn)情況下,關(guān)聯(lián)表的名稱使用多對(duì)多字段的名稱和包含這張表的模型的名稱以及Hash值生成,如:memberShip_person_3c1f5

若要想要手動(dòng)指定表的名稱,可以使用db_table關(guān)鍵字參數(shù)指定.

others

下列API和ForeignKey中的同名API相同.

  • ManyToManyField.db_constraint
  • ManyToManyField.related_name
  • ManyToManyField.related_query_name

使用ManyToManyField查詢

多對(duì)多關(guān)系和ForeignKey具有相似的API.

>>>e = Group.objects.get(id=3)
>>>e.members.all() # Returns all members objects for this Group.

反向查詢:

>>>a = Person.objects.get(id=1)
>>>a.group_set.all()

同樣related_name可以設(shè)置反向查詢集的名稱。

添加刪除關(guān)聯(lián)

因?yàn)镸anyToManyField自動(dòng)維護(hù)關(guān)聯(lián)表,程序員不便于直接訪問(wèn).ManyToManyField提供了API用于添加和刪除關(guān)聯(lián)(即through表中的記錄).

使用一個(gè)自動(dòng)維護(hù)through表的模型作為示例:

class User(models.Model):
  user_id = models.IntegerField(primary_key=True)
class Flight(models.Model):
  flight_id = models.IntegerField(primary_key=True)
  reserve = models.ManyToManyField(User, related_name='flight_reserve')

首先獲得要進(jìn)行關(guān)聯(lián)的Flight和User實(shí)例:

flights = Flight.objects.filter(flight_id=flight_id)
if flights.count() != 0:
  flight = flights[0]
users = User.objects.filter(id=user_id)
if users.count() != 0:
  user = users[0]

通過(guò)擁有關(guān)聯(lián)字段的Flight實(shí)例進(jìn)行添加關(guān)聯(lián)操作:

flight.reserve.add(user)
flight.save()

刪除操作與這類似:

flight.reserve.remove(user)
flight.save()

希望本文所述對(duì)大家基于Django框架的Python程序設(shè)計(jì)有所幫助。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI