程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Django learning notes - 13]: association query (date query, one-to-one query, one to many query, many to many query)

編輯:Python

List of articles

  • Django Configuration database utf-8 code
  • One 、 Date query
    • Add date field
    • Date field query
  • Two 、F、Q object
    • F object
    • Q object
  • 3、 ... and 、 Relational query
    • One to one relationship
    • One to many, one to one
    • Many-to-many queries

Django Configuration database utf-8 code

  1. settings.py Configure the database in the file
DATABASES = {

'default': {

'ENGINE': 'django.db.backends.mysql',
'NAME': 'dj2022',
'HOST': '127.0.0.1',
'PORT': 3306,
'USER': 'root',
'PASSWORD': '1064865165QAF',
'TEST': {

'CHARSET': 'utf-8',
'COLLATION': 'utf-8_general_ci'
}
}
}
SESSION_ENGINE = 'django.contrib.sessions.backends.db'

2. stay Navicat Enter the following command

CREATE DATABASE Database name DEFAULT SET utf8;


One 、 Date query

Add date field :
DateTimeField(verbose_name=‘’, auto_now=True)
auto_now Indicates that the current time is taken as its value

Add date field

  1. Create a model class


2、 After the database migration , Enter... In the terminal shell Environmental Science , Add field value


3、 stay Navicat Add data in

Date field query

1、 The query birthday is 1993-12-14

>>> Husband_1.objects.filter(birthday='1993-12-14')
<QuerySet [<Husband_1: Husband_1 object (1)>]>

2、 Inquire about 1991 Years later

>>> Husband_1.objects.filter(birthday__gt='1991-01-01')
<QuerySet [<Husband_1: Husband_1 object (1)>, <Husband_1: Husband_1 object (3)>, <Husband_1: Husband_1 object (4)>, <Husband_1: Husband_1 object (5)>]>

3、 Query only by year or month

>>> Husband_1.objects.filter(birthday__year__gt='1991')
<QuerySet [<Husband_1: Husband_1 object (1)>, <Husband_1: Husband_1 object (3)>, <Husband_1: Husband_1 object (4)>, <Husband_1: Husband_1 object (5)>]>

>>> Husband_1.objects.filter(birthday__month__gt='8')
<QuerySet [<Husband_1: Husband_1 object (1)>, <Husband_1: Husband_1 object (3)>, <Husband_1: Husband_1 object (4)>]>

Two 、F、Q object

F object

F object , Used to compare attributes with each other
Guide pack :from django.db.models import F
grammar :filter( Field name _ Operator =F(‘ Field name ’))

  • example : Query the data of twice the height and weight

>>> from django.db.models import F
>>> Husband_1.objects.filter(weight__lt=F('height')/2)
<QuerySet [<Husband_1: Husband_1 object (1)>, <Husband_1: Husband_1 object (2)>, <Husband_1: Husband_1 object (3)>, <Husband_1: Husband_1 object (4)>]>

Q object

Q object , For logical operation , And 、 or 、 Not
Guide pack :from django.db.models import Q

  1. example 1: The query is greater than 1993 Years or less 1992 Years of data
>>> Husband_1.objects.filter(Q(birthday__year__gt='1993')|Q(birthday__year__lt='1992'))
<QuerySet [<Husband_1: Husband_1 object (2)>, <Husband_1: Husband_1 object (3)>]>
  1. The query in addition to 1993 All the data for the year
>>> Husband_1.objects.filter(~Q(birthday__year__gt='1993'))
<QuerySet [<Husband_1: Husband_1 object (1)>, <Husband_1: Husband_1 object (2)>, <Husband_1: Husband_1 object (4)>, <Husband_1: Husband_1 object (5)>]>
  1. The query is greater than 1992 Years of age 33 The data of
>>> Husband_1.objects.filter(Q(birthday__year__gt='1992')&Q(age=33))
<QuerySet [<Husband_1: Husband_1 object (1)>, <Husband_1: Husband_1 object (5)>]>

or

>>> Husband_1.objects.filter(Q(birthday__year__gt='1992',age=33))
<QuerySet [<Husband_1: Husband_1 object (1)>, <Husband_1: Husband_1 object (5)>]>

3、 ... and 、 Relational query

One to one relationship

  1. Add model class
class Wife_1(models.Model):
name = models.CharField(verbose_name=' full name ', max_length=8)
age = models.IntegerField(verbose_name=' Age ')
height = models.FloatField(verbose_name=' height ')
weight = models.FloatField(verbose_name=' weight ', null=None)
husband = models.OneToOneField(Husband_1, verbose_name=' Her husband, ', on_delete=models.CASCADE)
class Meta:
db_table = 'Wife_1'

models.OneToOneField(Husband_1, verbose_name=‘ Her husband, ’, on_delete=models.CASCADE)
on_delete, Set cascading between tables
cascade : Link tables to tables , When the data in the main table is deleted , The data in the sub table will also be deleted , When making a one-to-one association , Must add on_delete Parameters

  1. Cascading demonstration

Binding table husband_1 in id by 3 The data of

>>> Wife_1.objects.create(name=' Xiaofang ', age=30, height=168, weight=48, husband_id=3)
<Wife_1: Wife_1 object (1)>
**>>> wife = Wife_1.objects.get(id=1)
>>> husband = Husband_1.objects.get(id=3)
>>> wife.husband
<Husband_1: Husband_1 object (3)>
>>> wife.husband.name
' Wang Wu '

One to many, one to one

  1. Add model class
class Children_1(models.Model):
Children_choice = (
(1, ' male '),
(2, ' Woman ')
)
name = models.CharField(verbose_name=' full name ', max_length=8)
age = models.IntegerField(verbose_name=' Age ')
sex = models.CharField(choices=Children_choice, verbose_name=' Gender ', max_length=2)
father = models.ForeignKey(Husband_1, on_delete=models.CASCADE)
mother = models.ForeignKey(Wife_1, on_delete=models.CASCADE)
class Meta:
db_table = 'Children_1'

2. Establishing a connection

>>> Children_1.objects.create(name=' Wang Xiaowu ', age=10, sex=' male ', father_id=3, mother_id=1)
<Children_1: Children_1 object (1)>
>>> Children_1.objects.create(name=' Wang Xiaomei ', age=8, sex=2, father_id=3, mother_id=1)
<Children_1: Children_1 object (2)>

3. Query demo

>>> son = Children_1.objects.get(id=1)
>>> daughter = Children_1.objects.get(id=2)
>>> son.mother
<Wife_1: Wife_1 object (1)>
>>> daughter.father.name
' Wang Wu '
  1. Use _set Do one to many queries

No, related_name When parameters are , from ‘ One ‘( Parents ) This party inquires about ’ many ‘( children ) On the side of , You need to add... After the lowercase name of the model class ’_set‘ The query , When there may be more than one demerit recorded in the query , You need to add... To the lower case of the model class name ’_set’ The query

>>> mother = Wife_1.objects.get(id=1)
>>> mother.children_1.set
>>> mother.children_1_set
<django.db.models.fields.related_descriptors.create_reverse_many_to_one_manager.<locals>.RelatedManager object at 0x0000026FE5696988>
>>> mother.children_1_set.all()
<QuerySet [<Children_1: Children_1 object (1)>, <Children_1: Children_1 object (2)>]>
>>> data1 = mother.children_1_set.all()
>>> data1[0].name
' Wang Xiaowu '
  1. Use related_name Parameters

related_name Parameters : Indicates an association name for the associated field , It's equivalent to a nickname , It is convenient for developers to query


>>> wife = Wife_1.objects.get(id=1)
>>> wife.mother
<django.db.models.fields.related_descriptors.create_reverse_many_to_one_manager.<locals>.RelatedManager object at 0x00000272C5CD68C8>
>>> data = wife.mother.all() # Get information about all children 
>>> data
<QuerySet [<Children_1: Children_1 object (1)>, <Children_1: Children_1 object (2)>]>
>>> data[1].name
' Wang Xiaomei '
>>> data[0].name
' Wang Xiaowu '

Many-to-many queries

  1. Add model class
class Brother_1(models.Model):
Brother_choice = (
(1, ' male '),
(2, ' Woman ')
)
name = models.CharField(verbose_name=' full name ', max_length=8)
age = models.IntegerField(verbose_name=' Age ')
sex = models.CharField(choices=Brother_choice, verbose_name=' Gender ', max_length=2)
brother = models.ManyToManyField(Husband_1, verbose_name=' brother ')
sister = models.ManyToManyField(Wife_1, verbose_name=' sister ')
class Meta:
db_table = 'brother_1'

Naming format of database tables : The model class name is lowercase _ Associated field name -> brother_1_brother

  1. The demonstration is as follows
>>> Brother_1.objects.create(name=' The sea ', age=35, sex=' male ')
<Brother_1: Brother_1 object (1)>
>>> brother = Brother_1.objects.get(id=1)
>>> husband = Husband_1.objects.get(id=1)
>>> brother.brother.set([husband,])


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved