【英文】Django项目中连接Mysql
Preface
Django project connecting to Mysql learning notes
Download dependencies
- Install Mysql driver
1 | pip install pymysql |
Modify global configuration
- Change the
DATABASES
configuration in the global configuration from the default sqlite3 configuration to the mysql configuration
ENGINE
: Database typeHOST
: Access address, leave it empty for127.0.0.1
by defaultPORT
: Port numberUSER
: Login usernamePASSWORD
: Login passwordNAME
: Name of the database to connect to
1 | DATABASES = { |
Initialize Mysql driver
1 | import pymysql |
Creating and Modifying Table Structure
Add a Mysql data table mapping in a Module
- Map Python class to Mysql data table
<app__name>_class_name
maps to Mysql data table name- The variables defined in the class map to the fields in the Mysql data table
Field types
CharField
: String typeTextField
: Text typeEmailField
: Email format stringUrlField
: URL format stringBooleanField
: Boolean typeNullBooleanField
: Nullable boolean typeIntegerField
: Integer type, value range[-2147483648,2147483647]
SmallIntegerField
: Short integer type[-32768,32767]
BigIntegerField
: Long integer typePositiveIntegerField
: Positive integer type, value range[0,2147483647]
PositiveSmallIntegerField
: Short positive integer type, value range[0,32767]
FloatField
: Floating point typeDecimalField
: DecimalDateField
: Date type, such asYYYY-MM-DD
DateTimeField
: Date and time type, such asYYYY-MM-DD hh:mm:ss
TimeField
: Time type, such ashh:mm:ss
ImageField
: Image type, used to store image format filesFileField
: File type, used to store files in any formatAttributes
Attributes that apply to the
DecimalField
field type
max_digits=<num>
: Maximum number of digits allowed in the numberdecimal_places=<num>
: Number of decimal places storedAttributes that apply to the
ImageField
field type
width_field=<num>
: Image widthheight_field=<num>
: Image heightAttributes that apply to the
ImageField
andFileField
field types
upload_to=<src>
: Local path for file uploadAttributes that apply to the
DateField
,DateTimeField
, andTimeField
field types
auto_now
: Automatically add the current time as the default valueauto_now_add
: Automatically add the current time as the default value when created for the first timeAttributes that apply to any field type
blank=True
: Whether it can be an empty valuenull=True
: Whether the value can be set to null, usually for the purpose of matching theon_delete
attributeprimary=True
: Whether it is a primary keymax_length=<num>
: Maximum lengthdefault=<value>
: Default valueverbose_name=<name>
: Name displayed in the admin of the backend management systemdb_column=<name>
: Database column nameunique=True
: Whether it is a unique index, a unique index does not allow duplicatesdb_index=True
: Whether it is a regular index, a regular index allows duplicates
1 | from django.db import models |
Define a composite index
1 | from django.db import models |
Fields for multiple table association
relate_name=""
: Customizable name for the current field, if not specified, the Python variable name is used as the field name by default
1 | from django.db import models |
Update Mysql data table structure
- Generate script: After defining the mapping, execute the command
python manage.py makemigrations