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

【Django學習筆記 - 13】:關聯查詢(日期查詢、一對一查詢、一對多查詢、多對多查詢)

編輯:Python

文章目錄

  • Django配置數據庫utf-8編碼
  • 一、日期查詢
    • 日期字段的添加
    • 日期字段的查詢
  • 二、F、Q對象
    • F對象
    • Q對象
  • 三、關聯查詢
    • 一對一關系
    • 一對多多對一
    • 多對多查詢

Django配置數據庫utf-8編碼

  1. settings.py文件中配置數據庫
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.在Navicat中輸入如下命令

CREATE DATABASE 數據庫名 DEFAULT SET utf8;


一、日期查詢

添加日期字段:
DateTimeField(verbose_name=‘’, auto_now=True)
auto_now 表示以現在的時間作為其值

日期字段的添加

  1. 創建一個模型類


2、進行數據庫的遷移後,在終端中進入shell環境,添加字段值


3、在Navicat中進行數據的添加

日期字段的查詢

1、查詢生日為1993-12-14

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

2、查詢1991年後的數據

>>> 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、只根據年代或月份查詢

>>> 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)>]>

二、F、Q對象

F對象

F對象,用於屬性與屬性之間進行比較
導包:from django.db.models import F
語法:filter(字段名_運算符=F(‘字段名’))

  • 例:查詢身高大體重兩倍的數據

>>> 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對象

Q對象,用於邏輯運算操作,與、或、非
導包:from django.db.models import Q

  1. 例1:查詢大於1993年或小於1992年的數據
>>> 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. 查詢除了1993年的所有數據
>>> 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. 查詢大於1992年且年齡為33的數據
>>> 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)>]>

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

三、關聯查詢

一對一關系

  1. 添加模型類
class Wife_1(models.Model):
name = models.CharField(verbose_name='姓名', max_length=8)
age = models.IntegerField(verbose_name='年齡')
height = models.FloatField(verbose_name='身高')
weight = models.FloatField(verbose_name='體重', null=None)
husband = models.OneToOneField(Husband_1, verbose_name='丈夫', on_delete=models.CASCADE)
class Meta:
db_table = 'Wife_1'

models.OneToOneField(Husband_1, verbose_name=‘丈夫’, on_delete=models.CASCADE)
on_delete,設置表與表之間的級聯
級聯:將表與表之間進行聯系,當主表中的數據刪除時,子表中的數據也會被刪除,在進行一對一的關聯時,必須添加上on_delete參數

  1. 級聯演示

綁定表husband_1中id為3的數據

>>> Wife_1.objects.create(name='小芳', 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
'王五'

一對多多對一

  1. 添加模型類
class Children_1(models.Model):
Children_choice = (
(1, '男'),
(2, '女')
)
name = models.CharField(verbose_name='姓名', max_length=8)
age = models.IntegerField(verbose_name='年齡')
sex = models.CharField(choices=Children_choice, verbose_name='性別', 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.建立連接

>>> Children_1.objects.create(name='王小五', age=10, sex='男', father_id=3, mother_id=1)
<Children_1: Children_1 object (1)>
>>> Children_1.objects.create(name='王小美', age=8, sex=2, father_id=3, mother_id=1)
<Children_1: Children_1 object (2)>

3.查詢演示

>>> 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
'王五'
  1. 使用_set進行一對多的查詢

沒有related_name參數時,從‘一‘(父母)這一方查詢’多‘(孩子)的一方,需要在模型類類名小寫後加上’_set‘進行查詢,當查詢的記過可能有多個的時候,就需要再模型類類名小寫後加上’_set’進行查詢

>>> 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
'王小五'
  1. 使用related_name參數

related_name參數:表示為關聯的字段起一個關聯名稱,相當於起別名,便於開發人員進行查詢


>>> 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() # 獲取所有孩子的信息
>>> data
<QuerySet [<Children_1: Children_1 object (1)>, <Children_1: Children_1 object (2)>]>
>>> data[1].name
'王小美'
>>> data[0].name
'王小五'

多對多查詢

  1. 添加模型類
class Brother_1(models.Model):
Brother_choice = (
(1, '男'),
(2, '女')
)
name = models.CharField(verbose_name='姓名', max_length=8)
age = models.IntegerField(verbose_name='年齡')
sex = models.CharField(choices=Brother_choice, verbose_name='性別', max_length=2)
brother = models.ManyToManyField(Husband_1, verbose_name='兄弟')
sister = models.ManyToManyField(Wife_1, verbose_name='姐妹')
class Meta:
db_table = 'brother_1'

數據庫表的命名格式:模型類名小寫_關聯字段名 -> brother_1_brother

  1. 演示如下
>>> Brother_1.objects.create(name='大海', age=35, sex='男')
<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