Django's QuerySet result set, two major features of lazy query, limited query set

QuerySet

1 Concept

The concept of query sets exists in Django's ORM.

Query set, also known as query result set, QuerySet , means a collection of objects obtained from the database.

When the following filter method is called, Django will return the query set (instead of a simple list):

  • all (): returns all data.
  • filter (): returns data that meets the conditions.
  • exclude (): returns data that meets the conditions.
  • order_by (): Sort the results.

You can call the filter again to filter the query set, such as

books = BookInfo.objects.filter(readcount__gt=30).order_by('pub_date')
books

<QuerySet [<BookInfo: 天龙八部>, <BookInfo: 雪山飞狐>]>

This means that the query set can contain zero, one or more filters. The filter limits the results of the query based on the given parameters.

From a SQL perspective, query sets are equivalent to select statements, and filters are like where, limit, and order by clauses.

Determine whether there is data in a query set:

  • exists (): Determine whether there is data in the query set, return True if there is, and False if not.

2 Two characteristics

1. Lazy execution

The query set is created without accessing the database, and the database is not accessed until the data is called. The case of calling the data includes iteration, serialization, and if.

For example, when the following statement is executed, no database query is performed, but a query set books is created

books = BookInfo.objects.all()

After continuing to perform the traversal iteration operation, the database query was really carried out

for book in books:
	print(book.name)
2. Cache

Using the same query set, the database query will occur when it is used for the first time, and then Django will cache the results. The cached data will be used when this query set is used again, reducing the number of database queries.

  • Situation 1: The following are two query sets, and the cache cannot be reused. Each query will interact with the database once, which increases the load on the database.

    from book.models import BookInfo
    
    [book.id for book in BookInfo.objects.all()]
    
    [book.id for book in BookInfo.objects.all()]
    

Insert picture description here
Insert picture description here

  • Case 2: After storage, the query set can be reused, and the data in the cache can be used a second time.

    books=BookInfo.objects.all()
    
    [book.id for book in books]
    
    [book.id for book in books]
    

Insert picture description here
Insert picture description here

3 Restrict query set

You can perform subscripting or slicing operations on the query set, which is equivalent to the limit and offset clauses in SQL.

注意:不支持负数索引。

After slicing the query set, a new query set is returned and the query will not be executed immediately.

If you get an object, use [0] directly, which is equivalent to [0: 1] .get (), but if there is no data, [0] raises IndexError exception, [0: 1] .get () raises DoesNotExist exception if there is no data .

  • Example: Get items 1 and 2 and run to view.

    books = BookInfo.objects.all()[0:2]
    books
    
    <QuerySet [<BookInfo: 射雕英雄传>, <BookInfo: 天龙八部>]>
    
Published 125 original articles · Like 260 · Visits 120,000+

Guess you like

Origin blog.csdn.net/weixin_44685869/article/details/105402339