当前位置:网站首页>[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
poetryManage the project environment .
relevantpoetryInstallation 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:DjangoWhen connecting to the database , Default linkdefaultDatabase under .ENGINE: There are multiple optionsdjango.db.backends.sqlite3django.db.backends.postgresqldjango.db.backends.mysqldjango.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、HOSTwait . Reference documents
Be careful : If you use
SQLiteOther 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.
QuestionThe model includes problem description and release time .ChoiceThe 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 :
DjangoYou can create database tables for applications (CREATE TABLE)DjangoCan be created withQuestionandChoiceObjects interactPythondatabaseAPI
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.pyfile , Change the model - function
python manage.py makemigrateionsGenerate migration files for model changes - function
python manage.py migrateApplication 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
DjangoSupport .Project start
DjangoSupport 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 .
边栏推荐
- kubernetes之ingress探索实践
- 数字信号处理——线性相位型(Ⅱ、Ⅳ型)FIR滤波器设计(2)
- Blocking sockets的读写操作该怎么玩?
- Compile and debug net6 source code
- 【邂逅Django】——(二)数据库配置
- Arm GIC (V) how arm TrustZone supports security interrupt analysis notes.
- 腾讯总考epoll, 很烦
- Friends day 2022
- JPA and criteria API - select only specific columns - JPA & criteria API - select only specific columns
- ASTM D 3801 vertical burning test of solid plastics
猜你喜欢

队列的链式存储
![[some notes]](/img/91/7657f90b50f012736579b1585b4ade.jpg)
[some notes]

【历史上的今天】7 月 1 日:分时系统之父诞生;支付宝推出条码支付;世界上第一支电视广告

How to install php7 and perform performance test using yum

Arm GIC (V) how arm TrustZone supports security interrupt analysis notes.

队列操作---

Onenet Internet of things platform - mqtt product equipment upload data points

I wish you all a happy reunion

被锡膏坑了一把
![[Suanli network] technological innovation of Suanli Network -- key technology of operation service](/img/80/6e3648c88d309516d4bc29db9c153c.jpg)
[Suanli network] technological innovation of Suanli Network -- key technology of operation service
随机推荐
ustime写出了bug
[some notes]
Accept different views with an open mind
[datawhale202206] pytorch recommendation system: recall model DSSM & youtubednn
System test UI test summary and questions (interview)
腾讯黎巍:深耕“监管科技”,护航数字经济行稳致远
Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation
R语言基于h2o包构建二分类模型:使用h2o.gbm构建梯度提升机模型GBM、使用h2o.auc计算模型的AUC值
[speech signal processing] 3 speech signal visualization -- prosody
[datawhale202206] pytorch recommendation system: multi task learning esmm & MMOE
本科毕业四年:工作,辞职,结婚,买房
6.30模拟赛总结
JPA and criteria API - select only specific columns - JPA & criteria API - select only specific columns
哪个券商公司开户佣金低又安全又可靠
双链表有关操作
Ipv6-6to4 experiment
Like the three foot platform
AI抠图工具
单点登录SSO与JWT好文整理
数字信号处理——线性相位型(Ⅱ、Ⅳ型)FIR滤波器设计(2)