当前位置:网站首页>[ORM framework]
[ORM framework]
2022-07-07 10:07:00 【chuntian_ tester】
Django ORM Framework operation database
Django ORM frame
Definition
- Map classes to data tables
- Through classes and objects, you can manipulate the data in its corresponding data table (CRUD)
step
Configure the database connection information
- Create databases and users
- Configuration database
- install mysqlclientIn the subapplication of models.py Define model classes in
- Generally in sub applications models.py Define model classes in
- Model classes must inherit Model Class or Model Subclasses of
- Define attributes in the model class ( It has to be for Field Subclass ), Equivalent to fields in the data table
- CharField–> varchar
- IntegerField–> integer
- BooleanField–>bool
class Animal(models.Model):
name = models.CharField(max_length=32)
age = models.IntegerField()
gender = models.BooleanField()
class Meta:
managed = True
Examples
1. Define model classes
class Projects(models.Model):
""" max_length: Must pass parameters verbose_name: When rendering forms , There will be a Chinese description below : Project name ; The background management site will also add a description to the current field help_text: stay api The interface document will be used as Chinese description information unique=True: Represents setting unique constraints on a field , The default is False default: Specify default TextField(): Support long text blank=True: Allow empty string ,DRF Valid only when deserializing input null = Tru: Allow for null,DRF Valid only when deserializing input DateTimeField Appoint auto_now_add=True, When creating a record, the time will be automatically created as the value of this field , Subsequent updates will not change this value DateTimeField Appoint auto_now=True, When updating a record , The time of updating the record will be automatically taken as the value of this field """
name = models.CharField(max_length=50,
verbose_name=' Project name ',
help_text=' Project name ',
unique=True)
leader = models.CharField(max_length=10,
verbose_name=' Project leader ',
help_text=' Project leader ')
# default=xxx Specify default
is_execute = models.BooleanField(verbose_name=' Whether to start the project ',
help_text=' Whether to start the project ',
default=False)
# TextField() Support long text
# blank=True, Allow empty string
# null = True, Allow for null
desc = models.TextField(verbose_name=' Project description information ',
help_text=' Project description information ',
blank=True, null=True, default='')
# DateTimeField Appoint auto_now_add=True, When creating a record, the time will be automatically created as the value of this field , Subsequent updates will not change this value
create_time = models.DateTimeField(auto_now_add=True,
verbose_name=' Creation time ',
help_text=' Creation time ')
# DateTimeField Appoint auto_now=True, When updating a record , The time of updating the record will be automatically taken as the value of this field
update_time = models.DateTimeField(auto_now=True,
verbose_name=' Update time ',
help_text=' Update time ')
# Modify the meta information of the model class
class Meta:
# # Whether it is managed
# managed = True
# db_table: Specified table name
db_table = 'tb_projects'
2. transfer
terminal Execute the command in the project root :
# Migrate all sub applications
python manage.py makemigrations
python manage.py migrate
# Migrate the specified sub application
python manage.py makemigrations Subapplication name
python manage.py migrate Subapplication name

The default name of the generated data table is : Subapplication name _ The model class name is lowercase
# View the generated migration script native sql sentence
python manage.py sqlmigrate Subapplication name (projects) Migration script name (0002_projects)

- Complete the database addition, deletion, modification and query operations through classes and objects
ORM Object composition
a. database : You need to manually create the database in advance
b. Data sheet : And ORM In the frame models.py Model classes in correspond one by one
c. Field : Class attributes in model classes
d. Record : Similar to multiple instances of model classes
Global profile settings.py Middle configuration database

ORM Modify the existing field names in the table
When we modify a field name or add a new field in the online database , Before deleting fields , All existing data should be backed up , Can be cold standby , It can be hot standby ;
- Cold standby :mysqldumps Orders can be made for cold standby
ORM Modify existing fields :
Modify the field name in the model class ( Generally, it can't be modified id Primary key )
Re execute the migration script
- python manage.py makemigrations Subapplication name - python manage.py migrate Subapplication name



边栏推荐
- web3.0系列之分布式存储IPFS
- Pit using BigDecimal
- 哈夫曼编码压缩文件
- ORM--分组查询,聚合查询,查询集QuerySet对象特性
- request对象对请求体,请求头参数的解析
- Horizontal split of database
- Official media attention! The list of top 100 domestic digital collection platforms was released, and the industry accelerated the healthy development of compliance
- Application of C # XML
- MySQL can connect locally through localhost or 127, but cannot connect through intranet IP (for example, Navicat connection reports an error of 1045 access denied for use...)
- Analyze Android event distribution mechanism according to popular interview questions (II) -- event conflict analysis and handling
猜你喜欢

【无标题】

Pit encountered by vs2015 under win7 (successful)

Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT

Pytest learning - dayone

Basic chapter: take you through notes

A wave of open source notebooks is coming

视频化全链路智能上云?一文详解什么是阿里云视频云「智能媒体生产」

反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释

Applet sliding, clicking and switching simple UI

使用BigDecimal的坑
随机推荐
CDZSC_ 2022 winter vacation personal training match level 21 (2)
为什么安装mysql时starting service报错?(操作系统-windows)
C# 初始化程序时查看初始化到哪里了示例
Detailed explanation of diffusion model
Qualifying 3
Pit encountered by vs2015 under win7 (successful)
Database multi table Association query problem
企业实战|复杂业务关系下的银行业运维指标体系建设
The applet realizes multi-level page switching back and forth, and supports sliding and clicking operations
2020ccpc Weihai J - Steins; Game (SG function, linear basis)
Google Colab装载Google Drive(Google Colab中使用Google Drive)
The Himalaya web version will pop up after each pause. It is recommended to download the client solution
2020 Zhejiang Provincial Games
web3.0系列之分布式存储IPFS
Finally, there is no need to change a line of code! Shardingsphere native driver comes out
Addition, deletion, modification and query of ThinkPHP database
arcgis操作:dwg数据转为shp数据
How to use Mongo shake to realize bidirectional synchronization of mongodb in shake database?
Flinkcdc failed to collect Oracle in the snapshot stage. How do you adjust this?
Writing file types generated by C language