python#django database one-to-one/one-to-many/many-to-many

One-to- oneOneToOneField user and user information

build

# 一对一
class   TestUser(models.Model):
    username=models.CharField(max_length=32)
    password = models.CharField(max_length=32)

class TestInfo(models.Model):
    mick_name=models.CharField(max_length=32)
    user=models.OneToOneField(to=TestUser,on_delete=models.CASCADE()#on_delete deleted mode CASCADE cascade delete

Then execute the database migration command

 

 Sync database

1Execute python manage.py makemigrations again

2Execute python manage.py migrate again

insert user

python .\manage.py shell

from user.models import TestUser,TestInfo

u=TestUser()   

u.username="Zhang San"   

u.password="123"

u.save()

Query the database and refresh it. Wait for the database insertion to be completed.

Insert user details

ui.mick_name="阿三"
ui.user=u    
ui.save()

Note that there is a delay in refreshing the database to view

Note: nick_name here is created by the author as mick_name

Query user details Query detailed user name

If there is a one-to-one field, use field query. If there is no field, use table name query.

#Query user information by user

>>> u=TestUser.objects.get(id=1)
>>> u.username
'Zhang San'
>>> u.testinfo #Note that the name of the database table in the database should be lowercase here
<TestInfo: TestInfo object (1) >

>>> u.testinfo.mick_name
'Ah San'

#Query user name through user information

>>> ui=TestInfo.objects.get(id=1)
>>> ui.user.username
'张三'

One-to-many ForeignKey students and classrooms

build

class ClassRoom(models.Model):
    r_number=models.CharField(max_length=32)#Classroom house number


# ForeignKey foreign key
class Student(models.Model):
    s_name=models.CharField(max_length=32)
    s_room=models.ForeignKey(to=ClassRoom,on_delete=models.CASCADE())#Foreign key field

 Sync database

1Execute python manage.py makemigrations again

2Execute python manage.py migrate again

Insert classroom Insert student

#Enter the command line terminal

python .\manage.py shell

>>> from user.models import Student,ClassRoom

#Insert classroom
>>> c=ClassRoom()
>>> c.r_number="1-2048"
>>> c.save()

#insert student xiaoming

>>> s=Student()
>>> s.s_name="小明"
>>> s.s_room=c
>>> s.save()

#insert student 小红

>>> s=Student()
>>> s.s_name="小红"
>>> s.s_room=c      
>>> c.save()        
>>> s.save()

#Insert a new small flower in classroom No. 2. Insert the small flower into classroom No. 2.

>>> c=ClassRoom()       
>>> c.r_number="1-2046"
>>> c.save

#Query Classroom 1 and Classroom 2

>>> c=ClassRoom.objects.get(id=1)  
>>> c1=ClassRoom.objects.get(id=2)
>>> s=Student()                    
>>> s.s_name="小花"                
>>> s.s_room=c1     
>>> s.save()

Query all students in the classroom

If there is a foreign key field, use the foreign key field. If there is no foreign key field, the table name is lowercase _set.

>>> c=ClassRoom()     

>>> c.student_set.all()
<QuerySet [<Student: Student object (1)>, <Student: Student object (2)>]>

#Query the first user and query the classroom number of the user through the classroom where the user is located.

>>> s=Student.objects.get(id=1)
>>> s.s_room                    
<ClassRoom: ClassRoom object (1)>
>>> s.s_room.r_number
'1-2048'

student (student table name in lowercase)_set forms one table to query multiple tables

Query the classroom corresponding to the student

There will be a linked list between many-to-many ManyToManyField teachers and classrooms

build

# many to many

# class
class ClassLevel(models.Model):
    c_name=models.CharField(max_length=32)

# teacher
class Teacher(models.Model):
    t_name=models.CharField(max_length=32)
    t_class=models.ManyToManyField(to=ClassLevel)

 Sync database

1Execute python manage.py makemigrations again

2Execute python manage.py migrate again

 

Add a classAdd a teacher

#Add class

>>> from user.models import ClassLevel

>>> from user.models import Teacher

>>> c=ClassLevel()

>>> c.c_name="python_0831"

>>> c.save()

#AddTeacherLaobian

>>>t=Teacher()

>>>t.t_name="老边"

>>> t.save()

#Add the teacher to the class python_0831
>>> t.t_class.add(c)
>>> t.save()

 

#Add Teacher Long Wen
>>> t=Teacher()
>>> t.t_name="Long Wen"
>>> t.save()

#Add the teacher to the class python_0831
>>> t.t_class.add(c)
>>> t.save()

class schedule

classroom table

Intermediate and linked list

Query all registered teachers in the class

#Query the first class

>>> c=ClassLevel.objects.get(id=1)
>>> c
<ClassLevel: ClassLevel object (1)>

#Get the class size
>>> c.teacher_set.all()
<QuerySet [<Teacher: Teacher object (1)>, <Teacher: Teacher object (2)>]>

#Loop through all class objects to get the teacher’s name
>>> [t.t_name for t in c.teacher_set.all()]
['Laobian', 'Longwen']

Check the classes taught by the teacher

>>> t=Teacher.objects.get(id=1)
>>> t.t_class.all()
<QuerySet [<ClassLevel: ClassLevel object (1)>]>
>>>

Guess you like

Origin blog.csdn.net/qq_35622606/article/details/132124417