Django uses the model to create the fields used by the database table

Source: Internet
Author: User

Django cannot create a database by using the model layer, but you can create a database table, the following are the fields that create the table and the parameters for the tables field.
First, the field
1, models. Autofield self-increment = Int (11)
If not, the default is to generate a column named ID, and if you want to display a custom self-increment column, you must set the column as the primary key primary_key=true.
2, models. Charfield string Field
Parameters must be max_length
3, models. Booleanfield Boolean type =tinyint (1)
Cannot be empty, blank=true
4, Models.comaseparatedintegerfield with comma-separated digital =varchar
Inherit Charfield, so the parameter must be max_lenght
5, models. Datefield Date Type Dates
For parameters, Auto_now =true updates the time for each update; Auto_now_add only creates the add for the first time, and then the update no longer changes.
6, models. Datetimefield Date Type DateTime
Parameters of the same Datefield
7, models. Decimal decimal Fractional type = Decimal
Integer digits max_digits and decimal digits must be specified decimal_places
8, models. Emailfield String type (regular expression mailbox) =varchar
To make a regular expression on a string
9, models. Floatfield floating-point type = Double
10, models. Integerfield Plastic Surgery
11, models. Bigintegerfield Long Plastic
Integer_field_ranges ={
' Smallintegerfield ':( -32768,32767),
' Integerfield ':( -2147483648,2147483647),
' Bigintegerfield ':( -9223372036854775808,9223372036854775807),
' Positivesmallintegerfield ':(0,32767),
' Positiveintegerfield ':(0,2147483647),
}
12, models. Ipaddressfield String type (IP4 regular expression)
13, models. Genericipaddressfield String Types (IP4 and IP6 are optional)
Parameter protocol can be: both, IPv4, IPv6
When verifying, the error is set according to the setting
14, models. Nullbooleanfield Boolean type allowed for null
15, models. Positiveintegerfiel positive integer
16, models. Positivesmallintegerfield Zheng Smallinteger
17, models. Slugfield minus, underline, letter, number
18, models. Smallintegerfield Digital
The fields in the database are: tinyint, smallint, int, bigint
19, models. TextField string =longtext
20, models. Timefield time Hh:mm[:ss[.uuuuuu]]
21, models. Urlfield string, address regular expression
22, models. Binaryfield binary
23, models. ImageField Pictures
24, models. Filepathfield file
Second, field parameters
1, Null=true
Whether a field in the database can be empty
2, Blank=true
Whether null values are allowed when adding data to the Django Admin
3, Primary_key = False
Primary key, after setting the primary key for Autofield, replaces the original self-increment ID column
4, Auto_now and Auto_now_add
Auto_now automatically created---whether added or modified, is the time of the current operation
Auto_now_add automatically created---is always the time when it was created
5, Choices
Gender_choice = (
(U ' M ', U ' Male '),
(U ' F ', U ' Female '),
)
Gender = models. Charfield (max_length=2,choices = gender_choice)
6, Max_length
7. Default Defaults
8, Verbose_name admin in the display name of the field
9. Field names in the Name|db_column database
10, Unique=true not allowed to repeat
11. Db_index = True Database index
12, Editable=true in the admin can be edited
13, error_messages=none error prompt
14, Auto_created=false automatic creation
15, Help_text in the admin tips help information
16, validators=[]
17, Upload-to

The procedure for creating a data table in Python code is as follows, and the process of creating a table is not the same as MySQL using SQL statements to build tables. In Django, create a table from the command-line python manage.py migrate by creating a class on the model layer, and then use the command Python manage.py to makemigrations the application table.

Django uses the model to create the fields used by the database table

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.