1024programmer Java How does django use mysql database to query the database_Mysql database usage (Django), mysql, usage, django

How does django use mysql database to query the database_Mysql database usage (Django), mysql, usage, django

One-to-many:

Many-party definition foreign key!!

# Student model class

class Student( AbstractUser):

mobile = models.CharField(max_length=11, unique=True, verbose_name='Mobile phone number')

cid = models.ForeignKey(to=”Class”, to_field=”id”)

class Meta:

db_table = 'tb_student'

verbose_name = 'user'

verbose_name_plural = verbose_name

# Class Model Class

class Class(models.Model):

id = models.AutoField(primary_key=True)

cname &# 61; models.CharField(max_length=32)

cdata = models.DateField()

Implementing many-to-one in django

When specifying

, the usage is as follows:

st = Student(

username=student_name,

password=password,

cid=cl # cl is an instance of Class class

)

In progress

When searching

# One-to-many/many-to-one

cl = Banji.objects.get(id& #61;3)

tea = cl.student_set.all()[0]

print(tea)

print(cl.student_set .first())

st = Student.objects.get(username='fenghua')

print(st.cid_id)

print(st.cid.cname)

Related query

1. Forward search (check more than 1):

Get through the foreign key of the model class (here is cid)

st.cid_id Find the class id corresponding to the student There is a field to_field=id when multiple parties define foreign keys in the model

st.cid.cname Find the class name corresponding to the student

2. Reverse search (1 check more):

If related_name is not set in the foreign key field& #xff0c;By default, the multi-party model lowercase _set is used.

If related_name=”students”, is set, students can be used directly for reverse query

cl (assumed to be a class object)

cl.Model name lower case_set.first()

cl.students.first()

Inner join query

By multiple model classes Conditional query – model class data

:

The syntax is as follows:

Associated model class name in lowercase__attribute name__conditional operator&# 61;Value

Note that :without the “__operator” part, means equal.

Example:

Query class, requires students to be “XX”

Class.objects.filter(student__username=

p>

'XX'

)

Query class, requires the names of students in the class to contain “X”X

Class.objects.filter(student__username__contains=

'X'

)

Conditionally query multiple model classes by one model class Data

:

(

Yes

Foreign key

Use foreign key as condition

)

The syntax is as follows:

Foreign key

__a model class attribute name__conditional operator=value

Note that :without the “__operator” part, means equal.

Example:

Query all students whose class is “XX”.

Student.objects.filter(

cid

__cname=

'天龙八部'

)

Query all students whose class size is greater than 30

Student.objects.filter(

cid

__cnum__gt=

30

)

Many-to-many search

Many-to-many operation

Forward query (query the class table from the teacher table)

# Class model class

class Banji(models.Model):

id = models .AutoField(primary_key=True)

cname = models.CharField(max_length=32)

cdata = models.DateField()

p>

cnum = models.IntegerField()

def __str__(self):

return “%s” % [self.__class__, self.cname]

class Meta:

db_table = 'tb_banji'

verbose_name = 'class&#39 ;

verbose_name_plural = verbose_name

# Teacher model class

class Teacher(models.Model):

id &# 61; models.AutoField(primary_key=True)

tname = models.CharField(max_length=32)

cid = models.ManyToManyField( to=”Banji”, name=”teacher”)

class Meta:

db_table = 'tb_teacher'

verbose_name = 'Teacher'

verbose_name_plural = verbose_name

# many-to-many

te & #61; Teacher.objects.get(id=4)

cl1 = te.teacher.all()[0]

cl2 = te .teacher.all()[1]

print(cl1.cname)

print(cl2.cname)

cl = Banji.objects .get(id=3)

te1 = cl.teacher_set.all()[0]

te2 = cl.teacher_set.all() [1]

print(te1.tname)

print(te2.tname)

Find out the teacher model class object. Many-to-many key (here is cid).all()

te.cid.all()

te.teacher.all()

teacher is the name attribute set by cid, Used for docking and searching for classes

Reverse query (reverse query of teacher table from class table)

Class model class object.Teacher model class lowercase_set

cl.teacher

_set

.all()

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/how-does-django-use-mysql-database-to-query-the-database_mysql-database-usage-django-mysql-usage-django/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索