Why does Django use Python code to define a data model?

Source: Internet
Author: User

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:

  1. Modify the model definition in models. py and use Python manage. py sqlall to view the new SQL statement.
  2. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.