Orm inert mechanism in Django

So we must first know what is ORM

Professional point of view: called object-relational mapping (Object-Relation Mapping) is a phenomenon not match each other in order to solve the existing object-oriented and relational database technology.

? What is that specific ORM is it :( in django, the code is automatically generated table class is also called the database according to --code first) ORM:

 

ORM between object-oriented model and relational model to bridge the gap.

By metadata mapping between the object and the database, the compiler automatically and transparently to the speech object persistence to the relational database, the database operations can be converted to the operation to the object

 

ORM has these advantages.

  • Data models are defined in one place, easier to update and maintain, but also conducive to code reuse.
  • There ORM tools available, many functions can be automated, such as data sanitization, pre-processing, transaction and so on.
  • It forces you to use MVC architecture, ORM is a natural Model, eventually make the code clearer.
  • ORM-based business code is relatively simple, less code, semantic, and easy to understand.
  • You do not have to write poorly performing SQL.

 

However, ORM is also very prominent shortcomings.

  • ORM library is not a lightweight tool, it takes a lot of effort to learn and settings.
  • For complex queries, ORM or is unable to express either perform as well as native SQL.
  • Abstract away the ORM database layer, developers can not understand the underlying database operations can not customize some special SQL.

 

Django inert mechanism

The so-called inert mechanism: Publisher.objects.all () or .filter () so only returns a QuerySet (query result set objects), it does not execute sql immediately, but only execute when calling the QuerySet.

The inert mechanism may iteration

1
2
3
# objs=models.Book.objects. all () # [obj1,obj2,ob3...]
for  obj  in  objs: # 每一obj就是一个行对象,此时会执行sql
#     print( "obj:" ,obj)

The inert mechanism can be sliced

1
2
3
4
# objs=models.Book.objects. all () # [obj1,obj2,ob3...]
# print(objs[1])
# print(objs[1:4])
# print(objs[::-1])

Django inert mechanism of caching problem

    Django has its own cache, the same if the object obj twice, the second time to check the value of value directly from the cache.

    If the content of the database changes made during, you will need to re-check value. Otherwise easily lead to dirty data.

    Can be directly used for the first time the object obj directly update operation, so for the next cycle time and re-execute the query database query operations, this time the cache also made a change; it can also be manually re-query results, but this is not recommended, because we do not know when the data will be modified properly or Django yourself to use data query data results

 

If the data was modified between 2 operations, the need to re-check values from the database, otherwise Django will remove the data from the cache, affect the final results .

Professional point of view: called object-relational mapping (Object-Relation Mapping) is a phenomenon not match each other in order to solve the existing object-oriented and relational database technology.

? What is that specific ORM is it :( in django, the code is automatically generated table class is also called the database according to --code first) ORM:

 

ORM between object-oriented model and relational model to bridge the gap.

By metadata mapping between the object and the database, the compiler automatically and transparently to the speech object persistence to the relational database, the database operations can be converted to the operation to the object

 

ORM has these advantages.

  • Data models are defined in one place, easier to update and maintain, but also conducive to code reuse.
  • There ORM tools available, many functions can be automated, such as data sanitization, pre-processing, transaction and so on.
  • It forces you to use MVC architecture, ORM is a natural Model, eventually make the code clearer.
  • ORM-based business code is relatively simple, less code, semantic, and easy to understand.
  • You do not have to write poorly performing SQL.

 

However, ORM is also very prominent shortcomings.

  • ORM library is not a lightweight tool, it takes a lot of effort to learn and settings.
  • For complex queries, ORM or is unable to express either perform as well as native SQL.
  • Abstract away the ORM database layer, developers can not understand the underlying database operations can not customize some special SQL.

 

Django inert mechanism

The so-called inert mechanism: Publisher.objects.all () or .filter () so only returns a QuerySet (query result set objects), it does not execute sql immediately, but only execute when calling the QuerySet.

The inert mechanism may iteration

1
2
3
# objs=models.Book.objects. all () # [obj1,obj2,ob3...]
for  obj  in  objs: # 每一obj就是一个行对象,此时会执行sql
#     print( "obj:" ,obj)

The inert mechanism can be sliced

1
2
3
4
# objs=models.Book.objects. all () # [obj1,obj2,ob3...]
# print(objs[1])
# print(objs[1:4])
# print(objs[::-1])

Django inert mechanism of caching problem

    Django has its own cache, the same if the object obj twice, the second time to check the value of value directly from the cache.

    If the content of the database changes made during, you will need to re-check value. Otherwise easily lead to dirty data.

    Can be directly used for the first time the object obj directly update operation, so for the next cycle time and re-execute the query database query operations, this time the cache also made a change; it can also be manually re-query results, but this is not recommended, because we do not know when the data will be modified properly or Django yourself to use data query data results

 

If the data was modified between 2 operations, the need to re-check values from the database, otherwise Django will remove the data from the cache, affect the final results .

Guess you like

Origin www.cnblogs.com/ls011218/p/11817131.html