Django QuerySet 初始化如何工作?

发布于 2024-10-13 08:53:25 字数 1739 浏览 3 评论 0原文

我指的是第一个答案中的代码片段 这篇文章:自定义查询集和管理器而不破坏 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月苍老的讽刺 2024-10-20 08:53:25

我不完全确定你在这里问什么。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文