django's built in to Mysql database table

Django operation Mysql database:

 

1.1 In the settings, configure the database parameters, so no need to modify, here we look at:

DATABASES = {

'default': {
# here may specify a database type to use, e.g. mysql

'ENGINE': 'django.db.backends.mysql',

'NAME': 'djangomysql',

'USER':'root',

'PASSWORD':'******',

'HOST':'localhost',

'PORT':'3306',

'OPTIONS': {'isolation_level': None}

}

}

1.2 Compile models.py Django model to define the specific form of self-defined python class, each model of the physical memory
in a manner that the python Class, each model represents a table in the database, each instance representing the class database
row of data, each variable represents the class of a field in the database.
By Django model, the python code and database operations combine to achieve encapsulation of the SQL query language. It
is said that you can not manage the database, SQL language can not, you also operated by the python code database
for. DJango operate the database through ORM, directly see the following code:


Models django.db Import from
# the Create your Models here Wallpaper.
class the UserInfo (models.Model):
'' '
to create two fields, the maximum length is 32, type char
' ''
User = models.CharField (MAX_LENGTH = 32)
pwd = models.CharField (max_length = 32)
here we create two fields were saved user name and password.
The above code corresponds to the following native SQL statements.
TABLE the UserInfo the CREATE (
"ID" Serial a PRIMARY KEY the NOT NULL,
"User" VARCHAR (30) the NOT NULL,
"pwd" VARCHAR (30) the NOT NULL
);


Note:
Django automatically create a default auto-increment primary key ID, of course, you can also specify the primary key yourself. The above SQL syntax and parameters based on PostgreSQL more fields
have some unique parameters for each field, for example, CharField need max_length parameter to specify the size of the VARCHAR database field. There are some general parameters apply to all fields. These parameters are defined in detail in the document, we learn here
learning about the most popular:
<1> CharField
string field for shorter string .CharField requires the existence of a parameter maxlength, and Django for the database layer from school posterior layer of the field limits the maximum number of characters allowed.
<2> IntegerField
for holding an integer.
<. 3> FloatField
a float must provide two parameters:
parameter description
max_digits total number of bits (excluding the sign and decimal point)
decimal_places decimal
for example, to save a maximum of 999 (decimal save two), you have to define this field:
models.FloatField (..., max_digits = 5, decimal_places = 2)
to save a maximum of one million (save 10 after the decimal point), you want to define this:
models.FloatField (..., max_digits = 19, decimal_places = 10)
ADMIN with a text box (<input type = "text" >) indicates that the field of preservation data.
<. 4> AutoField
a IntegerField, when adding a record it will automatically increase you normally no need to use this field;.
Customize a primary key: my_id = models.AutoField (primary_key = True )
If you do not specify the primary key, the system will automatically add a primary key field to your Model.
<. 5> BooleanField
a to true / field to false. admin checkbox represented by such field.
<. 6> the TextField
a large capacity of the text field.
ADMIN represented by a <textarea> (text area) the field data (a multi-line edit box)..
<. 7> EmailField
a CharField with legitimacy check Email, does not accept maxlength parameters.
<. 8> the DateField
a date field in total of the following additional optional parameters:.
the argument description
auto_now when the object is saved, the value of this field is automatically set to the current time is generally used to refer to "last-modified" timestamp.
auto_now_add when an object is first created, the value of this field is automatically set to the current time
usually to create an object that represents the time. (only meaningful in the admin ...)
<9> DateTimeField
A date-time field. Similar additional options DateField support the same.


More parameter
(. 1) null
If True, Django will be stored with NULL NULL value in the database. The default is False.
(. 1) blank
If True, this field is not allowed to fill. The default is False.
Note that this is different from null. null purely category database, data validation and blank areas.
If a field is blank = True, the validation will allow the form of the field is null. If the field is blank = False, the field is mandatory.
(2) default
field's default value. It can be a value or a callable object. If you can call, each time a new object is created it will be called.
(3) primary_key
If True, then the field is the primary key for the model. If you do not specify any field primary_key True =,
Django will automatically add a primary key IntegerField field to do so unless you want to override the default behavior of the primary key,
otherwise no need to set up a primary_key any field = True.
(. 4) UNIQUE
If the value is set to True, the value of the data field in the entire table must be unique
(. 5) choices
an iterator object is composed of a tuple (e.g., lists or tuples), the fields used to provide options. If the choices, the default form will be a select box instead of the standard text box, and this option is selected the box choices in options.

 

In the end we need to create a table is created in the teminal pycharm in the command table in the database, there are two commands, namely:
generate database synchronization script code
python manage.py makemigrations
by running makemigrations command, the equivalent of telling Django, you model there are changes, and you want to save these changes as a "migration" (migration).
migrations to save Django model is modified record files, which are stored on the disk, we can open the file to look at, which is readable and preservation of content that can be compiled to help us at any time manually.

And then synchronize the database:
Python manage.py the migrate

Guess you like

Origin www.cnblogs.com/Jokerguigui/p/11606564.html