Creating a link to a database in Django and invoking data from the database is a critical step, so how do you implement this process?
The following article simply combs through the process and application of creating the model layer
Model -models
First you need to understand what a model is?
A class created by the structure of a data table in a database
1. Ways to create and use models
1) Create database needs to be created manually in database ...
2) Configure setting.py in configuration
DATABASES ={
' Default ': {
' ENGINE ':
' NAME ':},
}
Parsing of parameters:
1. Engine: Engines Django.db.backends.mysql
2. Name: The database names to connect to Web.db
3.USER: User name, usually root
4. PASSWORD: Password
5. Host: Connected hosts, native LOCALHOST/127.0.0.1/can not write
6. Port: Ports, 3306
For example, the following picture:
When doing a database cluster, if you want to connect to multiple databases, you can write the default after then add a dictionary type of Key-value pair,
The name of the key can be defined by itself.
2. Writing Models
First you need to understand two concepts, entity classes and entities
1) Entity class
Each class in models is called a model class or entity class (Entry)
Each entity class in models must inherit from models. Model (native class is no models operation)
2) Entity:
A row of records in a data table is an entity.
Entity integrity: Ensure that the data in each table cannot be duplicated.
The primary key is one of the ways to implement entity integrity.
Data field and field options available in 3.Django
Author here only write some of the more commonly used, more detailed usage details can go to the official website: www.djangoproject.com
Model Layer
1. Data fields
1) Charfield () string, max_length must be written
2) Booleanfield () boolean value
3) DataField () time
4) Datetimefield () time
5) Decimalfield () fixed-point number with decimal point
6) Emailfield () email type
7) Urlfield () URL type, converted to a fixed-length (200) string in the database
8) Filefield () file type
9) Intergerfield () integral type number
) Floatfield () floating point number
One) ImageField () picture, usually the address of the image stored in the actual application
TextField () text
2. Field Options
1) Null #是否允许为空, assignment true or false
2) Blank #是否为空格
3) Choices
4) Db_column #属性名
5) Db_index Index
6) Default Defaults
7) Primary_key primary key
Django Basic Article--models