SoFunction
Updated on 2024-10-29

Django implementation of a one-to-many table model of the cross-table query method

When there are two tables, for example a student table and a class table, it is a many-to-one relationship.

Method 1:

c = (pk=1)
#query to class with ID 1
stus = (cls=c)
# Find out all the students whose class is c
#stus is more than one object
# Can loop through the results
for stu in stus :
 print ,

Method 2

stus = (cls__name='1703')
#Double underscores are cross-table queries,Query all of the student's correspondingclsbecause of1703schoolchildren

Method 3

c=(pk=1)
# Get the class with ID 1
stus=c.Student_set.all
#Get all students under the class

html page get method

<td>{{ line.student_set.count }}</td>

Above this Django realize a pair of multi-table model of cross-table query method is all I share with you, I hope to give you a reference, and I hope you support me more.