14. Django MTV and Django model

MTV

We may have heard about the MVC pattern. MVC is the model (model) - view (view) - Abbreviation controller (Controller), and a software design model, with a service logic, data, isolated tissue interface display method code. Django also has its design patterns, which we call MTV.

  • M represents a model (Model), i.e., the data access layer. This layer handles all transactions related to the data
  • T represents the template (Template), ie, the presentation layer. Such as HTML
  • V represents a view (View), i.e., the business logic layer. We write local business code.

Django model

When running our first project and did not involve the operation of the database, if we want to create a table, then how does it work? We can operate in the article / models.py below. Some students might say, models.py not a python file? Have anything to do with the database, because of the special nature of Django, so we can use the object-relational mapping (ORM, Object Relational Mapping), simply means that the form of the operation database operations, then how does it work? We still use the blog taken as an example, assume that blog have a table with title, author, article content, Published composed, then how do we generate a data table in Django below it? We opened article / models.py file, enter the following code:

from django.db import models


# Create your models here.
class Article(models.Model):
    title = models.CharField(max_length=10, ) # 标题
    author = models.CharField(max_length=10) # 作者
    content = models.CharField(max_length=500) # 内容

Article we create a class that inherits models.Model, defined four fields: title, author, content date_publish, CharField, DateTimeField is the type of the field, in addition to these two, Django and many other data types in the following table:

Types of description
Auto Field For storing digital type integer.
BooleanField Boolean type for storing data (Ture or False)
CharField For storing character data, it is necessary to specify the length max_length.
CommaSeparatedIntegerField A comma-separated integer type data is stored.
DateField Date type, must be "YYYY-MM-DD" format
DateTimeField Date and time type, must be "YYYY-MM-DD HH: MM [: ss [.uuuuuu]] [TZ]" format.
DecimalField Decimal type for storing a decimal number.
EmailField E-mail type
FilePathField Class file path type, FilePathFields must have either 'allow_files' or' allow_folders set to True.
FloatField Float. For storing floating point data.
IntegerField For storing digital type integer.
BigIntegerField Used to store large integer types of digital, the maximum number of support: 9223372036854775807
GenericIPAddressField Storing the IP address type, IPv4, and the IPv6 address, the format string.
NullBooleanField vlaue must be either None, True or False.
PositiveIntegerField Positive integer
PositiveSmallIntegerField Positive small integer
SlugField Max_length need to define value.
SmallIntegerField Small integer
TextField For storing text data type.
TimeField Time type. "HH: MM [: ss [.uuuuuu]]" format
URLField Used to store the URL address
BinaryField Raw binary data

But now the article is just a class, not a table, so we have to execute the following two commands, perform a database migration

F:\新建文件夹\blog>python manage.py makemigrations article
Migrations for 'article':
  article\migrations\0001_initial.py
    - Create model Article

F:\新建文件夹\blog>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, article, auth, contenttypes, sessions
Running migrations:
  No migrations to apply.

python manage.py makemigrations articleEquivalent to establish migrations directory in this app, and all your changes on modes.py of record, such as 0001_initial.py, but this change is not applied to the database file
python manage.py migratethe changes applied to the database file, such as generating table like .

Guess you like

Origin www.cnblogs.com/suim1218/p/11009582.html