Django The model is similar to java In language jpa equally , Reduce data for creating data , Simplify operations in the developing database .
from django.db import models
from django.urls import reverse
# Create your models here.
class UserBaseInfo(models.Model):
id = models.AutoField(verbose_name=' Number ', primary_key=True)
username = models.CharField(verbose_name=' User name ', max_length=30)
password = models.CharField(verbose_name=' password ', max_length=20)
status = models.CharField(verbose_name=' state ', max_length=1)
createdate = models.DateTimeField(
verbose_name=' Date of creation ', db_column='createDate')
def __str__(self):
return str(self.id)
class Meta:
verbose_name = ' Basic information of personnel '
db_table = 'UserBaseInfo4'
Attribute comments :
DecimalField:
Set the return value of the model
Used for definition Django Model behavior
Parameters :
to The model name
to_field Associated field name
on_delete Deleted configuration
models.OneToOneField()
Configure database connection tool , Default is used sqlite3, Here it is modified as mysql, Logs are configured .
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'localhost',
'POST': '3306',
# Remove foreign key constraint
'OPTIONS': {
"init_command": "SET foreign_key_checks = 0;",
},
}
}
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django.db.backends': {
'handlers': ['console'],
'propagate': True,
'level': 'DEBUG',
}
},
}
Then generate the corresponding data table
python manage.py makemigrations
python manage.py migrate
In [1]: from app3.models import *
In [2]: users = UserBaseInfos.objects.all()
In [3]: print(users[0].username)
(0.000) SELECT @@SQL_AUTO_IS_NULL; args=None
(0.000) SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; args=None
(0.000) SELECT `UserBaseInfo4`.`id`, `UserBaseInfo4`.`username`, `UserBaseInfo4`.`password`, `UserBaseInfo4`.`status`, `UserBaseInfo4`.`createDate` FROM `UserBaseInfo4` LIMIT 1; args=()
Background wall
users = UserBaseInfos.objects.get(id=1)
print(users.username)
values()
Extract the corresponding field
distinct()
users = UserBaseInfos.objects.distinct().values(‘department’)
Use sava() Save updated data ,create() You can also add data .
users = UserBaseInfos.objects.get(id=1)
users.username = "Django.js"
users.save()
delete() Method to delete data
F Function can update fields , Use after update refresh_from_db()
user.salary = F(“salary”) + 1000
Q Function can support conditional query
filter(Q(age__gt=30)&Q(salary__gt=5000))
Use django.db.models.Manager.raw() perform SQL, You can also execute with parameters SQL sentence , Use [] To pass in parameters .
users = UserBaseInfos.objects.raw("select * from UserBaseInfo4")
print(users[0].username)
The transaction ASID characteristic : Atomicity 、 Uniformity 、 Isolation and persistence
Django There are mainly decorator mode and with sentence .
@transaction.atomic
def trans(request):
save_id = transaction.savepoint()
try:
---
transaction.savepoint_commit(save_id)
except:
transaction.savepoint_rollback(save_id)
Python The decorators in are i
explain : This is a practical