Create a third table to establish themselves-many relationship
Create a table
# Teachers and students table tables may be one-many relationship, you can create a third table associated manually when construction of the table class Student (models.Model): name = models.CharField (= max_length 32 , null = True) Age models.CharField = (= MAX_LENGTH 32 , null = True) class Teacher (models.Model): name = models.CharField (= MAX_LENGTH 32 , null = True) Gender = models.CharField (= MAX_LENGTH 32 , null = True) # the establishment of the third table, two tables together before the association class TeacherToStudent (models.Model): STU= models.ForeignKey('Student', null=True) teac = models.ForeignKey('Teacher', null=True)
CRUD operations
# Increase directly by models. .Objects.create class name added models.Student.objects.create (name = " xxx " , Age = 12 ) models.Teacher.objects.create (name = " OOO " , Gender = " M " ) models.TeacherToStudent.objects.create (stu_id = . 1 , teac_id = . 1 ) # identify objects deleted .delete (), and delete the correspondence relationship models.Teacher.objects.filter (ID = . 4 ) .Delete ( ) models.TeacherToStudent.objects.filter (teac_id = . 4 ) .Delete () # change as different tables operated separately models.Teacher.objects.filter (id= 3 ) .Update (name = " XOXO " ) # check, if I am asking for a student to be taught at the same time the number of teachers, there are three ways to chart at # 1 : RES = models.Student.objects.filter (the above mentioned id = 2 ) .all () # to identify eligible students for row in RES: Print (row.name) r = row.teachertostudent_set.all () # (reverse lookup) tables represent a third of all students with id 2 rows for i in r: # i.teac # (n check) indicates that the corresponding table teacher of a print (i.teac.name) # for all teachers identify student id 2 corresponds to # 2 : RES = models.TeacherToStudent.objects .filter (stu__name = " CaO" ) .All () # check out the names of the students for all rows (magical double underline) cao corresponding for Row in RES: Print (row.stu.name, row.teac.name) # find students and teachers correspondence between # . 3 : RES = models.TeacherToStudent.objects.filter (stu__name = " Li " ) .values ( " teac__name " ) # QuerySet check out list, which contains a dictionary print (res)
Django method to create many relationship
Create a table
# Create tables, create a class and course-many relationship #-many relationship is created by django in ManyToManyField, two columns on the field can be any of a class the Classes (models.Model): name = Models. as CharField (= MAX_LENGTH 32 , null = True) Course = models.ManyToManyField ( ' Course, ' ) class Course, (models.Model): name = models.CharField (= MAX_LENGTH 32 , null = True)
CRUD operations
# Increase must first find the corresponding class or course objects obj = models.Classes.objects.filter (id = 4 ) .first () added via course.add when construction of the table after # is found, take courses on the list id was beaten pass in obj.course.add ( 1 ) # add a obj.course.add ( * [ 2 , 3 ]) # increase the number # delete found objects. attribute name .clear when construction of the table () delete all obj = models.Classes.objects.filter (= ID . 4 ) .first () obj.course.clear () # reset (changed), the object is found by. SET () method, which put a list obj = Models .Classes.objects.filter (= ID . 3 ) .first () obj.course. SET ([ . 1 , 2 , . 3]) # Check for tables that have ManyToManyField model class field RES = models.Classes.objects.all () # to detect all objects for Row in RES: Print (row.name) for I in row.course .all (): # row.course.all () is that all program objects each corresponding class Print (i.name) # ManyToManyField is no table model class field for RES = models.Course.objects.all ( ) # is the first to detect all objects for Row in RES: Print ( " =================== " ) Print (row.name) for i in row.classes_set .all (): # corresponding to each discipline all class print (i.name)
Note: Both methods establish many relationships are possible, taking into account the points which, if the relationship between the tables with a simple,
Third table does not require adding fields except the two tables can be created with id django But if table relationships are more complex,
And, third table also need to add some other fields, we need to use our custom-many relationship (
Manually create a third table), the structure can be customized third table