Django,哪个功能属于QuerySet和Manager? [英] Django, which function belongs to QuerySet and Manager?
问题描述
我曾经认为QuerySet方法返回QuerySet实例,但显然没有.
I used to think QuerySet method return QuerySet instances, but it apparently not.
例如,count()是一个查询集方法,而不是管理者的
For instance, count() is a queryset method not manager's
我该如何确定哪些函数用于自定义QuerySet,哪些函数用于自定义Manager类?
How do I decide which functions go to custom QuerySet and which go to custom Manager class?
推荐答案
能够访问管理器上的count()
和查询集上的某些功能是很有意义的.这使您可以执行以下操作:
It makes sense to be able to access some functions like count()
on the manager and the queryset. This allows you to do:
Blog.objects.count() # total number of blogs
Blog.objects.filter(status='PUBLISHED').count() # Number of published blogs
Django具有方法 as_manager
,可让您从自定义查询集中创建管理器.这意味着您不必在管理器和查询集上重复您的方法.
Django has a method as_manager
which allows you to create a manager from a custom queryset. This means you don't have to duplicate your methods on the manager and queryset.
这篇关于Django,哪个功能属于QuerySet和Manager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!