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;
Add date field :
DateTimeField(verbose_name=‘’, auto_now=True)
auto_now Indicates that the current time is taken as its value
2、 After the database migration , Enter... In the terminal shell Environmental Science , Add field value
3、 stay Navicat Add data in
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)>]>
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 ’))
>>> 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 , For logical operation , And 、 or 、 Not
Guide pack :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)>]>
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)>]>
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
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 '
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 '
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 '
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 '
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
>>> 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,])