Tags: data efault reg MAP Filter Benefits Auto Complete mod Migration
First, models (module) Knowledge expansion
The concept of the
- ORM (manager), ORM refers to the object-relationship-mapping abbreviation, is to map the database table into a class, the class attribute represents the field, the benefit is more
operability for the developer, In the case of database pruning, you can manipulate objects directly without considering specific SQL statements. The internal implementation of the interaction is the manager auto-completion, when defining the class must inherit the model class in the models module, so that the class is a modeling class. Currently, models supports three major databases of SQLite, MySQL and Oracle. But this also has a certain
disadvantage, that is, the internal conversion process may take some time, making the efficiency is not so high.
- The first step before using models is to change the database to use in the configuration file and add the current application to the Installed_app entry in the configuration file. When the field type and properties are defined, Makemigrations generates the migration file, is an internal conversion SQL statement, and finally executes the migration file in migrate to import the data.
- Multiple table relationship types are divided into
one-to-many: foreignkey--fields are defined on multiple sides
Many-to-many: manytomanyfield--defines fields at both ends
one-to-one: onetoonefield-- Define a field in any section of
- to deal with a one-to-many relationship (take the hero book as an example) because the field is defined at one end, you can use the hero.book_id as long as there are no fields because there is no field, so you should use Book.heroinfo _set gets all the heroes in the book
- tittle= models when the field is defined. Charfield () is a field option written in parentheses, such as max_length=20 is a maximum length of 20 and a variety of field options, such as default setting defaults, NULL, to decorate the field
- meta-option Meta class, This class is defined in the model class and is called a meta class in Python. The role of this class here is to set meta-information, such as the name of the table can be changed db_table= ' book ' and Sort, and so on, the table name defaults to the application name _ model class
- the benefit of creating multiple apps in a project is to increase the reusability of the code
- and then add and remove changes to a table , you can use the table to generate an object, manipulate the information in the table with the object, and finally call the Save () method to save the manager in the
- A manager is a property in a model class that is used to map between a model class and a data table, using the object Manager by default.
- This manager can also be defined by ourselves, through the definition of a class, and then inherit the manager in the models module, by rewriting or new definition inside the method can also implement the manager function.
- In Django, we can also use multiple managers, using the model class to refer to the managed method directly, and which manager to refer to.
- There are two benefits of redefining the manager
First, you can filter the results after the query so that they return different result sets
Second, it is easier to create model objects that can be used instead of defining the Create class method in the model class itself (because the __init__ object method cannot be defined in the Model Class). In the manager
The Creat method is defined in the definition and is then invoked directly by the object.
- About the query
- In the process of using model classes, it is certain that the data in the table is queried and uploaded to the foreground for display. And the way to implement and write SQL query statement is essentially no different.
is to return a query set, and we can still filter, aggregate, and a series of SQL operations before returning the query set.
- The method of returning a query set is called a filter, with all (), filter (), exclude (), order_by (), values (), and many more methods
One of the longest uses is all (return all), filter () filters before returning
- The use of the filter process will use a comparison, including a number of operational relationships, specifically in the filter (write here), where two different fields to introduce the F object
- The manager can also directly query the results using the aggregate aggregate (max, etc.)
- The conditions and relationships in filter () are implemented with filter (condition 1), filter (condition 2), or filter (condition 1, Condition 2)
And or the relationship to introduce Q object, through Q (condition 1) | Q (condition 2) to achieve