What is a Django QuerySet?

后端 未结 4 1009
轻奢々
轻奢々 2020-12-14 00:15

When I do this,

>>> b = Blog.objects.all()
>>> b

I get this:

>>>[,

        
4条回答
  •  再見小時候
    2020-12-14 00:38

    A django queryset is like its name says, basically a collection of (sql) queries, in your example above print(b.query) will show you the sql query generated from your django filter calls.

    Since querysets are lazy, the database query isn't done immediately, but only when needed - when the queryset is evaluated. This happens for example if you call its __str__ method when you print it, if you would call list() on it, or, what happens mostly, you iterate over it (for post in b..). This lazyness should save you from doing unnecessary queries and also allows you to chain querysets and filters for example (you can filter a queryset as often as you want to).

提交回复
热议问题