We are not ORM such operation of the database:
- Open Database Connection conn = .....
- Create a cursor cursor = conn.cursor ()
- 执行SQL effect_rows=cursor.excute("update `recordinfo_user` ......")
- Commit changes conn.commit ()
- Close the cursor cursor.close ()
- Close the connection conn.Close ()
To improve efficiency, people invented a: Python code can be converted into a tool --ORM SQL statements.
The so-called object-relational mapping means:
The python object mapping database tables to implement the programmer to concentrate on business logic, the database without concern for the specific operation. Object-relational mapping, reflected in the M Django in MTV model (model layer), we have to learn is how to use python code to define a database table.
model:
- Model mapped to a table in the database
- Model is a class in Django python
- Models inherited from django.models.Model
- Each field represents a column in the database model
Model Definition:
- Create a model class in the application of the model file models.py
- In the project configuration file registration applications in settings.py
- [To be performed once after each change model a] is performed in the terminal command to save the changes recorded python manage.py makemigrations
- [To be performed once after each change of a model] python manage.py migrate executed in the terminal operation command to the database
Model fields:
- Field performance in python class attribute, the database table columns
- To avoid python field named keywords, reserved words commonly used form of xx_xx
- Do not be two consecutive underscores field names, because __ is django query syntax
Field Type:
Note that the field is a type, Django uses the built-in field types hump naming rules
- BooleanField Boolean values Type Default None
- CharField string type must receive a parameter setting the maximum length max_length
- DateField Date Type
- EmailField default mailbox type max_length = 254 Django built verify the legitimacy of mail
- FileField upload type
- IntegerField signed integer type ~ number range -2147483648 2147483647
- TextField large amounts of text types