Django QuerySet 初始化如何工作?
我指的是第一个答案中的代码片段 这篇文章:自定义查询集和管理器而不破坏 DRY?
from django.db import models
from django.db.models.query import QuerySet
class CustomQuerySetManager(models.Manager):
"""A re-usable Manager to access a custom QuerySet"""
def __getattr__(self, attr, *args):
try:
return getattr(self.__class__, attr, *args)
except AttributeError:
return getattr(self.get_query_set(), attr, *args)
def get_query_set(self):
return self.model.QuerySet(self.model)
这里是model:
from custom_queryset.models import CustomQuerySetManager
from django.db.models.query import QuerySet
class Inquiry(models.Model):
objects = CustomQuerySetManager()
class QuerySet(QuerySet):
def active_for_account(self, account):
self.filter(account = account, deleted=False, *args, **kwargs)
self.model.QuerySet(self.model) 始终接收相同的模型,但结果 QuerySet 取决于输入 QuerySet。例如:
Inquiry.objects.all()[:5].active_for_account(xyz)
,则 active_for_account
将收到包含 5 个项目的查询集,而在 Inquiry.objects.all()[:7].active_for_account(xyz)
,active_for_account
将收到包含 7 个项目的查询集。以下是堆栈跟踪:
Inquiry.objects.all()[:5].active_for_account(xyz)
return getattr(self.get_query_set(), attr, *args),
return self.model.QuerySet(self.model) (1)
Inquiry.objects.all()[:7].active_for_account(xyz)
return getattr(self.get_query_set(), attr, *args),
return self.model.QuerySet(self.model) (2)
为什么 (1) 和 (2) 的结果不同?
I'm referring to the code snippet in the first answer taken from
this post: Custom QuerySet and Manager without breaking DRY?
from django.db import models
from django.db.models.query import QuerySet
class CustomQuerySetManager(models.Manager):
"""A re-usable Manager to access a custom QuerySet"""
def __getattr__(self, attr, *args):
try:
return getattr(self.__class__, attr, *args)
except AttributeError:
return getattr(self.get_query_set(), attr, *args)
def get_query_set(self):
return self.model.QuerySet(self.model)
Here is the model:
from custom_queryset.models import CustomQuerySetManager
from django.db.models.query import QuerySet
class Inquiry(models.Model):
objects = CustomQuerySetManager()
class QuerySet(QuerySet):
def active_for_account(self, account):
self.filter(account = account, deleted=False, *args, **kwargs)
self.model.QuerySet(self.model)
always receives a same model, but the result QuerySet depends on the input QuerySet. For example:
Inquiry.objects.all()[:5].active_for_account(xyz)
, then active_for_account
will receive a query set of 5 items, while in Inquiry.objects.all()[:7].active_for_account(xyz)
, active_for_account
will receive a query set of 7 items. Here are stack traces:
Inquiry.objects.all()[:5].active_for_account(xyz)
return getattr(self.get_query_set(), attr, *args),
return self.model.QuerySet(self.model) (1)
Inquiry.objects.all()[:7].active_for_account(xyz)
return getattr(self.get_query_set(), attr, *args),
return self.model.QuerySet(self.model) (2)
Why are results at (1) and (2) different?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不完全确定你在这里问什么。
Inquiry.objects.all()[:5]
不会为您提供给定对象,它会为您提供一个包含五个元素的 QuerySet 对象。I'm not entirely sure what you're asking here.
Inquiry.objects.all()[:5]
doesn't give you give objects, it gives you a single QuerySet object which contains five elements.