Django Basic Article--models

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.