django模型系統(三)

1,自定義主鍵字段的建立java

  AutoFiled(pirmary_key=True)  # 通常不會自定義python

2,order_by  asc  descmysql

表關係sql

OneToOne數據庫

student = models.OneToOneField('Student' , on_delete=models.CASCADE)  #一對一的核心代碼

(mysite) pyvip@VIP:~/python34/mysite$ python manage.py makemigrations   #根目錄下運行blog

(mysite) pyvip@VIP:~/python34/mysite$ python manage.py migrateip

(py3env) pyvip@VIP:~$ mysql -A mysite -uroot -pqwe123  #進入數據庫rem

 

OneToManyit

grade = models.ForeignKey('Grade', on_delete=models.SET_NULL, null=True)  #一對多核心代碼

 

ManyToManyio

 

2,關聯表的數據操做

  OneToMany

正向 :一個模型若是定義了一個外鍵字段,經過這個模型操做外鍵

 刪除必須有null

增刪改查

 

In [29]: g3 = Grade.objects.create(name='進階', num='40期')

In [30]: g3.student_set.create(name='李鵬輝')
Out[30]: <Student: 李鵬輝-0>

In [31]: g3.student_set.add(s)    add馬上立刻執行,不用操做數據庫

 

 有clear方法先執行clear再添加,若是clear沒有就直接添加

 只要是返現就模型的小寫加上s-set

Many-to-Many

*****指定了中間表,add, remove, set 都不能用,必須用中間表

正向:

In [78]: c1.students.all()
Out[78]: <QuerySet [<Student: 心藍-0>, <Student: 心藍-0>, <Student: 李鵬輝-0>]>

反向:

In [79]: s1.course_set.all()
Out[79]: <QuerySet [<Course: python全棧>, <Course: java>, <Course: python全棧>]>

 

One—to—One

In [85]: sd = StudentDetail.objects.create(num='2019020001', college='家裏蹲', student=s1)

In [86]: sd
Out[86]: <StudentDetail: StudentDetail object (1)>

In [87]: sd.student
Out[87]: <Student: 心藍-0>

In [88]: sd.student.name
Out[88]: '心藍'

In [89]: sd.student.age
Out[89]: 0

In [90]: s1.studentdetail
Out[90]: <StudentDetail: StudentDetail object (1)>

In [91]: s1.studentdetail.num
Out[91]: '2019020001'

In [92]: s1.studentdetail.college
Out[92]: '家裏蹲'

 

跨表查詢

 

In [93]: res = Course.objects.filter(students__sex=1)

In [94]: print(res.query)
SELECT `teacher_course`.`id`, `teacher_course`.`name` FROM `teacher_course` INNER JOIN `teacher_enroll` ON (`teacher_course`.`id` = `teacher_enroll`.`course_id`) INNER JOIN `teacher_student` ON (`teacher_enroll`.`student_id` = `teacher_student`.`id`) WHERE `teacher_student`.`sex` = 1

 

In [95]: res = Student.objects.filter(course__name__contain='python')   

In [96]: print(res.query)
SELECT `teacher_student`.`id`, `teacher_student`.`name`, `teacher_student`.`age`, `teacher_student`.`sex`, `teacher_student`.`qq`, `teacher_student`.`phone`, `teacher_student`.`grade_id`, `teacher_student`.`c_time`, `teacher_student`.`e_tine` FROM `teacher_student` INNER JOIN `teacher_enroll` ON (`teacher_student`.`id` = `teacher_enroll`.`student_id`) INNER JOIN `teacher_course` ON (`teacher_enroll`.`course_id` = `teacher_course`.`id`) WHERE `teacher_course`.`name` LIKE BINARY %python%

 

In [99]: res = Student.objects.filter(course__name__contains='english',grade__num__contains='33')

In [100]: print(res.query)
SELECT `teacher_student`.`id`, `teacher_student`.`name`, `teacher_student`.`age`, `teacher_student`.`sex`, `teacher_student`.`qq`, `teacher_student`.`phone`, `teacher_student`.`grade_id`, `teacher_student`.`c_time`, `teacher_student`.`e_tine` FROM `teacher_student` INNER JOIN `teacher_enroll` ON (`teacher_student`.`id` = `teacher_enroll`.`student_id`) INNER JOIN `teacher_course` ON (`teacher_enroll`.`course_id` = `teacher_course`.`id`) INNER JOIN `teacher_grade` ON (`teacher_student`.`grade_id` = `teacher_grade`.`id`) WHERE (`teacher_course`.`name` LIKE BINARY %english% AND `teacher_grade`.`num` LIKE BINARY %33%)

 

In [101]: res = Student.objects.filter(enroll__pay__lt=3000)

In [102]: print(res.query)
SELECT `teacher_student`.`id`, `teacher_student`.`name`, `teacher_student`.`age`, `teacher_student`.`sex`, `teacher_student`.`qq`, `teacher_student`.`phone`, `teacher_student`.`grade_id`, `teacher_student`.`c_time`, `teacher_student`.`e_tine` FROM `teacher_student` INNER JOIN `teacher_enroll` ON (`teacher_student`.`id` = `teacher_enroll`.`student_id`) WHERE `teacher_enroll`.`pay` < 3000.0

 

In [103]: res = Grade.objects.filter(student__course__name__contains='python')

In [104]: print(res.query) SELECT `teacher_grade`.`id`, `teacher_grade`.`name`, `teacher_grade`.`num` FROM `teacher_grade` INNER JOIN `teacher_student` ON (`teacher_grade`.`id` = `teacher_student`.`grade_id`) INNER JOIN `teacher_enroll` ON (`teacher_student`.`id` = `teacher_enroll`.`student_id`) INNER JOIN `teacher_course` ON (`teacher_enroll`.`course_id` = `teacher_course`.`id`) WHERE `teacher_course`.`name` LIKE BINARY %python%

相關文章
相關標籤/搜索