關聯對象操做及多表查詢python
關聯表的數據操做:數據庫
一對多:django
正向:若是一個模型有外鍵字段,經過這個模型對外鍵進行操做叫作正向。框架
更新:spa
經過屬性賦值對象
In [1]: from teacher.models import Student In [2]: from teacher.models import Grade In [3]: g=Grade.objects.create(name='django框架',num='7') In [4]: g Out[4]: <Grade: Grade object (1)> In [5]: g=Grade.objects.first() In [6]: s=Student.objects.first() In [7]: s.grade=g In [8]: s.save()
經過主鍵的方式blog
In [9]: s2=Student.objects.last() In [10]: s2.grade_id=g.id In [11]: s2.save()
總結:Foreignkey字段的更新,跟普通字段沒什麼區別。rem
刪:只有外鍵設置了null=True,你就能夠經過賦值None來刪除關係get
In [15]: s.grade=None In [16]: s.save()
查:it
In [18]: res=Student.objects.filter(grade__name='django框架') In [19]: print(res.query) SELECT `teacher_student`.`id`, `teacher_student`.`name`, `teacher_student`.`age`, `teacher_student`.`sex`, `teacher_student`.`qq`, `teacher_student`.`phone`, `teacher_student`.`c_time`, `teacher_student`.`grade_id` FROM `teacher_student` INNER JOIN `teacher_grade` ON (`teacher_student`.`grade_id` = `teacher_grade`.`id`) WHERE `teacher_grade`.`name` = django框架
過濾Foreignkey字段模型的字段
反向:一個模型若是被另一個模型外鍵關聯,經過這個模型對關聯它的模型進行操做叫作反向。
查:經過管理器,默認管理器是有外鍵的模型名稱的小寫加上_set(foo_set,foo是模型名稱小寫),經過這個管理器能夠查詢模型的實例,在定義外鍵的時候,經過related_name能夠覆蓋這個名稱。
grade=models.ForeignKey('Grade',on_delete=models.SET_NULL,null=True,related_name='students1') In [2]: from teacher.models import Grade,Student In [3]: g=Grade.objects.first() In [4]: g Out[4]: <Grade: django框架> In [6]: g.students1.all() Out[6]: <QuerySet [<Student: 趙柳>]>
增:
經過add方法,能夠添加多個
In [11]: new_s=Student.objects.first()
In [13]: g.students1.add(new_s)
In [17]: s1=Student.objects.get(pk=2)
In [18]: s2=Student.objects.get(pk=3)
In [19]: g.students1.add(s1,s2)
經過create方法
In [22]: g.students1.create(name='郭富城') Out[22]: <Student: 郭富城>
刪:刪掉關係
remove(obj1,obj2,obj3)
In [23]: gfc=Student.objects.last() In [24]: g.students1.remove(gfc)
clear() 清空
In [25]: g.students1.clear()
add,remove,clear直接操做數據庫
改:替換對象集
set([s1,s2])
In [25]: s1 Out[25]: <Student: 李四> In [26]: s2 Out[26]: <Student: 王五> In [27]: g.students1.set([s1,s2])
多對多:
若是由於有額外字段,自定義了中間模型,咱們就須要經過中間模型的管理器進行manytomany關係的建立和刪除
默認狀況跟一對多中的add,create,remove,clear用法一致
惟一的區別是多對多正向的時候,多對多字段就是一個管理器。
反向的時候,跟一對多的方向一致,也是在模型小寫後面加上_set,和一對多相似,同樣能夠經過定義related_name能夠覆蓋這個名稱。
In [1]: from teacher.models import Course,Enroll In [2]: c1=Course.objects.create(name='python全棧') In [4]: c2=Course.objects.create(name='python全套') In [10]: from teacher.models import Course,Enroll,Student In [11]: s1=Student.objects.create(name='范冰冰') In [12]: s2=Student.objects.create(name='李冰冰') In [13]: s1 Out[13]: <Student: 范冰冰> In [14]: s2 Out[14]: <Student: 李冰冰> In [15]: e=Enroll() In [16]: e.student=s1 In [17]: e.course=c1 In [18]: e.paid=6680 In [19]: e.save() In [20]: c1 Out[20]: <Course: python全棧> In [21]: c1.students.all() Out[21]: <QuerySet [<Student: 范冰冰>]> In [22]: c1.students.filter(name='范冰冰') Out[22]: <QuerySet [<Student: 范冰冰>]> In [23]: c1.students.get(name='范冰冰') 、Out[23]: <Student: 范冰冰> In [24]: s1 Out[24]: <Student: 范冰冰> In [25]: s1.course_set.filter(name__startswith='python') Out[25]: <QuerySet [<Course: python全棧>]>
一對一:
很是相似一對多字段,增刪改查和普通字段沒有什麼區別
反向的時候,使用模型的小寫,也能夠給related_name覆蓋,這個就不是管理器,就是一個普通屬性。
注意:一個被一對一管理的模型,他的實例若是沒有被分配關係。舉個例子:學生對象,沒有分配一個學生詳情對象。若是去取,全拋出異常DoseNotExist。
跨表查詢: 要跨越關係,只需使用跨越模型的相關字段名,如下劃線分隔,直到達到你想要的字段爲止。
例如:查詢男生都報名了什麼課程
res=Course.objects.filter(students__sex=1).distinct()
這個關係要多深就有多深
例:查詢全部報名了python課的學員
res=Student.objects.filter(course__name__contains='python')
例:查詢全部報名python全棧課程,在django框架第七期班級的學員
res=Student.objects.filter(course__name='python全棧',grade__name='django框架',grade__num='7')
例:學員報名了python課程的班級有哪些
res=Grade.objects.filter(students1__course__name__contains='python').distinct()