M in MTV represents the model. The Django model defines data in the database in the form of Python code. For the data layer, it is equivalent to the create table statement. It only executes Python code instead of SQL, and contains more meanings than database field definitions. Django uses the model to execute SQL code in the background and describe the result using the python data structure. Django also uses models to present advanced concepts that cannot be processed by SQL.
1 from django.db import models
2
3 class Publisher(models.Model):
4 name = models.CharField(max_length=30)
5 address = models.CharField(max_length=50)
6 city = models.CharField(max_length=30)
7 website = models.URLField()
If you are familiar with databases, you may immediately think that using python to define data models is a little redundant? Django does this for the following reasons:
1. introspection (automatically identifying the database during Operation) will lead to overload and data integrity problems. To provide convenient data access APIs, Django needs to know the internal information of the database layer in some way. There are two implementation methods. The first method is to use python to clearly define the data model, and the second method is to automatically detect and identify the data model through introspection.
The second method looks clearer, because the data table information is only stored in one place-database, but it may cause some problems. First, scanning the database during running will cause severe System overload. If each request needs to scan the table structure of the database, or even once the service is started, it will lead to unacceptable system overload. (Some people think that this level of system overload is acceptable, and Django developers aim to minimize the system overload of the Framework ). Second, some databases, especially old MySQL versions, do not fully store the precise introspection metadata.
2. It is very interesting to write Python code. Keeping thinking in Python will prevent your brain from switching back and forth in different fields. Keeping it in a single programming environment/ideology can help you increase productivity. I have to repeat SQL, write Python code, and then write SQL ,..., It will split your head.
3. Express the data model in code so that you can easily control the version of the data model. In this way, you can easily understand the changes in the data layer.
4. SQL can only describe specific data fields. For example, most databases do not have dedicated field types to describe email addresses and URLs. This can be done with the Django model. The advantage is that advanced data types bring higher efficiency and better code reuse.
5. compatibility issues with SQL on different database platforms. When publishing a web application, using the python module to describe the database structure information can avoid writing different create tables for MySQL, PostgreSQL, and SQLite.
Of course, this method also has a disadvantage, that is, the synchronization of Python code and database tables. If you have modified a Django model, you need to modify the database on your own to ensure synchronization with the model.
$ Python manage. py syncdb // you cannot modify or delete a model and synchronize it to the database.
$ Python manage. py dbshell // automatically checks which command line client is used according to the settings in database_server
When you modify or delete a data model, the preceding command does not take effect (only when a new model or app is added ). Therefore, it is safe to execute the preceding command because it does not repeatedly execute the defined SQL.
The following policies are generally used to solve this problem:
- Modify the model definition in models. py and use Python manage. py sqlall to view the new SQL statement.
- Use alter table on the dbshell or DB client to add, delete, modify, and delete fields.
Finally, we will remind you that Django provides a practical tool to automatically scan and generate models from existing database tables. This is quick and useful for existing databases.