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 表示以現在的時間作為其值
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對象,用於屬性與屬性之間進行比較
導包: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對象,用於邏輯運算操作,與、或、非
導包:from django.db.models import Q
>>> 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)>]>
>>> 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)>]>
>>> 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)>]>
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參數
綁定表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
'王五'
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
'王五'
沒有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
'王小五'
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
'王小五'
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
>>> 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,])