当前位置:网站首页>[encounter Django] - (II) database configuration
[encounter Django] - (II) database configuration
2022-07-01 12:36:00 【51CTO】
to encounter Django - Catalog
Part 1:【 to encounter Django】—— ( One ) Create project
Part 2:【 to encounter Django】—— ( Two ) Database configuration
️ Part 3:【 to encounter Django】—— ( 3、 ... and ) View
️ Part 4:【 to encounter Django】—— ( Four ) Forms and common views
️ Part 5:【 to encounter Django】—— ( 5、 ... and ) Perfect interface ( Customize interfaces and styles )
️ Part 6:【 to encounter Django】—— ( 6、 ... and ) User defined management interface
️ Part 7:【 to encounter Django】—— ( 7、 ... and ) automated testing
List of articles
- to encounter Django - Catalog
- Preface
- One 、 Project profile `settings.py`
- 1.1 Database configuration
- 1.2 Time zone and language
- 1.3 `Django` The default self-contained application profile
- Two 、 Creating models
- 3、 ... and 、 Activate the model
- Four 、 First try API
- 5、 ... and 、`Djaong` Management interface
- 5.1 Create an administrator account
- 5.2 Login management interface
- 5.3 Add voting application to the management page
- 5.4 Experience convenient management functions
- 6、 ... and 、【`PyCharm` Use tips 】
- 6.1 Use `PyCharm` Tools for running `makemigrations & migrate`
- 6.2 Use `PyCharm` function `django shell`
- 7、 ... and 、 The simplest beautification Django Admin
- summary
Preface
This series of articles , stay Django
Under the basic template of the official document tutorial , Some improvements and deletions have been made , Added some of my own insights .
I hope that after reading this series of articles , Yes Django
Be able to have a clear understanding .
It's a long way to go , I will go up and down !
Django
Official documents : https://www.djangoproject.com/
Learning process , Read more official documents , Can solve many problems
This tutorial USES
poetry
Manage the project environment .
relevantpoetry
Installation and use of , Please refer to 【Python - A virtual environment 】 The start of the project , Start by isolating the development environment _CoderChaos Technology blog _51CTO Blog
One 、 Project profile settings.py
1.1 Database configuration
mysite/settings.py
, It's a that includes Django
Project settings Python
modular .
Configuration of database , It's the variables DATABASES
.
Django
By default SQLite
As the default database .Python
built-in SQLite
, So you don't need to install extra things to use .
If you do a real project , Better not to use SQLite
.
Parameter description :
default
:Django
When connecting to the database , Default linkdefault
Database under .ENGINE
: There are multiple optionsdjango.db.backends.sqlite3
django.db.backends.postgresql
django.db.backends.mysql
django.db.backends.oracle
- Other third-party database backend . Reference address
NAME
: Database name . If you useSQLite
, The database will be a file on the computer . The default value isBASE_DIR / 'db.sqlite3'
, The database will be stored in the root directory of the project .- The other parameters : If not used
SQLite
, You must add some additional settings , such asUSER
、PASSWORD
、HOST
wait . Reference documents
Be careful : If you use
SQLite
Other databases , You need to make sure that you have created the database before using it . You can use it in your database interactive command lineCREATE DATABASE database_name;
To complete the creation of the database .
1.2 Time zone and language
Can be in settings.py
In file , Modify time zone and language .
1.3 Django
The default self-contained application profile
settings.py
Some notes in the document :INSTALLED_APPS
INSTALLED_APPS The default includes the following Django Built in application of :
django.contrib.admin
: Administrator sitedjango.contrib.auth
: Authentication and authorization systemdjango.contrib.contenttypes
: Content type frameworkdjango.contrib.sessions
: Conversational frameworkdjango.contrib.messages
: Message framedjango.contrib.staticfiles
: Framework for managing static files
Some applications opened by default need at least one data table , therefore , Before using them, you need to create some tables in the database . The following commands need to be executed :python manage.py migrate
Two 、 Creating models
Defining models , That is, database structure design and additional metadata .
In this voting application , You need to create two models : problem Question
And options Choice
.
Question
The model includes problem description and release time .Choice
The model includes the option description and the current number of votes . Each option belongs to a question .
Creating models , Inherit django.db.models.Model
; Each model has many class variables , Both represent a data table field in the model .
Each field is Field
Class .
explain :
Django
You can create database tables for applications (CREATE TABLE
)Django
Can be created withQuestion
andChoice
Objects interactPython
databaseAPI
3、 ... and 、 Activate the model
Django
The application is “ Pluggable ” Of .
add to polls
application
Now? ,Django
The project already contains polls
application .
Run the command makemigrations
, Perform a migration :python manage.py makemigrations
function makemigrations
command ,Django
It will detect the modification of the model file , And store the modified part as a migration . Migration is Django
A storage of data structure changes .
migrate
Is a command that automatically performs database migration and synchronously manages the database structure .
therefore , perform makemigrations
after , To modify the synchronization in the database , Need to execute again python manage.py migrate
.
Migration is a very powerful feature , It can continuously change the database structure during the development process without deleting tables and recreating tables .
It focuses on smooth database upgrade without data loss .
Changing the model requires the following steps :
- edit
models.py
file , Change the model - function
python manage.py makemigrateions
Generate migration files for model changes - function
python manage.py migrate
Application database migration
Four 、 First try API
4.1 Django
Basic use of interactive commands
Get into Django
Interactive command line :python manage.py shell
about Question
Returned display data , You can edit Question
Modify the model code , to Question
and Chioce
increase __str__()
Method .
Once again into the Django Interactive command line :python manage.py shell
4.2 Add custom methods to the model
Once again into the Django Interactive command line :python manage.py shell
5、 ... and 、Djaong
Management interface
The management interface is not for visitors to the website , But for managers .
5.1 Create an administrator account
Run the following command on the command line :python manage.py createsuperuser
You will be prompted later , enter one user name 、 mailbox 、 password 、 Confirm the password .
5.2 Login management interface
Start development server :python manage.py runserver
Open the browser : http://127.0.0.1:8000/admin/
Enter your account and password , You can enter the management interface .
5.3 Add voting application to the management page
5.4 Experience convenient management functions
Click the button in the page , You can use the add / delete / modify query function .
6、 ... and 、【PyCharm
Use tips 】
6.1 Use PyCharm
Tools for running makemigrations & migrate
6.2 Use PyCharm
function django shell
stay PyCharm
Bottom toolbar , choice Python Console
You can enter python manage.py shell
.
Prerequisite : Project start
Django
Support .Project start
Django
Support see : 【 to encounter Django】——( One ) Create project _CoderChaos Technology blog _51CTO BlogFour 、【PyCharm Use tips 】
Compared with from Terminal
adopt python manage.py shell
Get into Django shell
, There will be a certain prompt when entering .
7、 ... and 、 The simplest beautification Django Admin
7.1 simpleui
brief introduction
Django Admin The default interface design language has some shortcomings , For example, the color is single , The use of a large number of lines , Segmentation is obvious . Categorizing these deficiencies is the monotonous interface 、 The similarity is obvious 、 Lack of surprises .
simpleui
: Based on a vue+element-ui Of django admin The theme of modernization .
GitHub Address : https://github.com/newpanjing/simpleui
7.2 simpleui
Use
7.2.1 install
7.2.2 Start using
After installation , In my own project settings.py
In file INSTALLED_APPS
Of first line
Join in simpleui.
If the previous service is still running , Refresh the page .
summary
This article briefly introduces Django
Connection and use with database , Use of the default background management interface .
And the use of PyCharm
Quick operation makemigrations & migrate
Command and django shell
, Use django-simpleui
Beautify the default background management interface .
边栏推荐
- Double linked list related operations
- Share several tools for designing exquisite circuit diagrams
- 项目部署,一点也不难!
- (混更一篇)多个txt文本转一个表格
- Onenet Internet of things platform - mqtt product equipment upload data points
- 被锡膏坑了一把
- Onenet Internet of things platform - mqtt product devices send messages to message queues MQ
- Circular linked list--
- 使用nvm管理nodejs(把高版本降级为低版本)
- Pandas reads MySQL data
猜你喜欢
【历史上的今天】7 月 1 日:分时系统之父诞生;支付宝推出条码支付;世界上第一支电视广告
IOS interview
[Suanli network] technological innovation of Suanli Network -- key technology of operation service
Ipv6-6to4 experiment
How to use opcache, an optimization acceleration component of PHP
Onenet Internet of things platform - mqtt product devices send messages to message queues MQ
[some notes]
Queue operation---
STM32 project practice (1) introduction and use of photosensitive resistor
Onenet Internet of things platform - create mqtts products and devices
随机推荐
Onenet Internet of things platform - the console sends commands to mqtt product devices
[datawhale202206] pytorch recommendation system: precision model deepfm & DIN
Chain storage of binary tree
ustime写出了bug
ASP. Net core 6 from entry to enterprise level practical development application technology summary
ANSI/UL 94 VTM薄质材料垂直燃烧测试
数字信号处理——线性相位型(Ⅱ、Ⅳ型)FIR滤波器设计(2)
A hole in solder paste
Question d'entrevue de Huawei: recrutement
MySQL的零拷贝技术
JS reverse | m3u8 data decryption of a spring and autumn network
[brain opening] west tide and going to the world series
[JS] interview questions
阿霍的三个阶段
【MAUI】为 Label、Image 等控件添加点击事件
顺序表有关操作
kubernetes之ingress探索实践
leetcode:226. 翻转二叉树【dfs翻转】
redis探索之缓存击穿、缓存雪崩、缓存穿透
Tencent Li Wei: deeply cultivate "regulatory technology" to escort the steady and long-term development of the digital economy