自定义可链接查询集

发布于 2024-11-02 05:49:37 字数 1545 浏览 1 评论 0原文

这是我的一段代码

from django.db import models
from django.db.models.query import QuerySet
from mptt.models import MPTTModel
from base.models import Content, OrderedContent


class ArgumentQuerySet(QuerySet):
    def statements_with_count(self, *args, **kwargs):
        from statement.models import Statement
        statements = Statement.objects.none()
        result = self
        for node in result:
            statements_node = Statement.objects.filter( arguments__in = node.get_descendants(include_self = True), *args, **kwargs ).distinct()
            statements |= statements_node
            setattr(node, 'count', statements_node.count())
        statements = statements.distinct()
        setattr(result, 'statements', statements)
        setattr(result, 'count', statements.count())
        return result


class ArgumentManager(models.Manager):
    def get_query_set(self):
        return ArgumentQuerySet(self.model)


class Argument(MPTTModel, OrderedContent):
    parent = models.ForeignKey('self', null=True, blank=True)
    objects = ArgumentManager()

    class MPTTMeta:
        parent_attr = 'parent'
        order_insertion_by = ['weight', 'title', ]

    def __unicode__(self):
        return self.title

以下命令给了我关注的结果

a = Argument.objects.filter( title__icontains = 'function' )
b = a.statements_with_count()

否则,以下命令不起作用

c = Argument.objects.get( id = 514 )
d = c.get_children()
e = d.statements_with_count()

我该如何解决这个问题?

This is a piece of my code

from django.db import models
from django.db.models.query import QuerySet
from mptt.models import MPTTModel
from base.models import Content, OrderedContent


class ArgumentQuerySet(QuerySet):
    def statements_with_count(self, *args, **kwargs):
        from statement.models import Statement
        statements = Statement.objects.none()
        result = self
        for node in result:
            statements_node = Statement.objects.filter( arguments__in = node.get_descendants(include_self = True), *args, **kwargs ).distinct()
            statements |= statements_node
            setattr(node, 'count', statements_node.count())
        statements = statements.distinct()
        setattr(result, 'statements', statements)
        setattr(result, 'count', statements.count())
        return result


class ArgumentManager(models.Manager):
    def get_query_set(self):
        return ArgumentQuerySet(self.model)


class Argument(MPTTModel, OrderedContent):
    parent = models.ForeignKey('self', null=True, blank=True)
    objects = ArgumentManager()

    class MPTTMeta:
        parent_attr = 'parent'
        order_insertion_by = ['weight', 'title', ]

    def __unicode__(self):
        return self.title

The following commands gives me the attended result

a = Argument.objects.filter( title__icontains = 'function' )
b = a.statements_with_count()

Otherwise, the following commands doesn't work

c = Argument.objects.get( id = 514 )
d = c.get_children()
e = d.statements_with_count()

How can i fix this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

倦话 2024-11-09 05:49:37

MPTT 使用 TreeManager 实例作为 get_children,它返回一个 models.query.Queryset,而不是您的自定义子类。您应该执行以下操作:

argument = Argument.objects.get(id = 514)
children_ids = [a.pk for a in argument.get_children()]
result = Argument.objects.filter(pk__in=children_ids).statements_with_count()

请注意,您的statements_with_count方法的性能不是很好,因为它将导致每个对象产生一个额外的数据库计数。

MPTT uses the TreeManager instance for get_children which returns a models.query.Queryset, not your custom subclass of it. You chould do somethling like:

argument = Argument.objects.get(id = 514)
children_ids = [a.pk for a in argument.get_children()]
result = Argument.objects.filter(pk__in=children_ids).statements_with_count()

Note that your statements_with_count method is not very performant, as it will cause one additional database count for each object.

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