PythonWeb-Django framework model learning -Demo5- layer: field model and

We are not ORM such operation of the database:

  1. Open Database Connection conn = .....
  2. Create a cursor cursor = conn.cursor ()
  3. 执行SQL effect_rows=cursor.excute("update `recordinfo_user` ......")
  4. Commit changes conn.commit ()
  5. Close the cursor cursor.close ()
  6. 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:

  1. Model mapped to a table in the database
  2. Model is a class in Django python
  3. Models inherited from django.models.Model
  4. Each field represents a column in the database model

Model Definition:

  1. Create a model class in the application of the model file models.py
  2. In the project configuration file registration applications in settings.py
  3. [To be performed once after each change model a] is performed in the terminal command to save the changes recorded python manage.py makemigrations
  4. [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:

  1. Field performance in python class attribute, the database table columns
  2. To avoid python field named keywords, reserved words commonly used form of xx_xx
  3. 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

  1. BooleanField Boolean values ​​Type Default None
  2. CharField string type must receive a parameter setting the maximum length max_length
  3. DateField Date Type
  4. EmailField default mailbox type max_length = 254 Django built verify the legitimacy of mail
  5. FileField upload type
  6. IntegerField signed integer type ~ number range -2147483648 2147483647
  7. TextField large amounts of text types

Guess you like

Origin www.cnblogs.com/bigbosscyb/p/12511099.html