自定义可链接查询集
这是我的一段代码
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MPTT 使用
TreeManager
实例作为get_children
,它返回一个models.query.Queryset
,而不是您的自定义子类。您应该执行以下操作:请注意,您的statements_with_count方法的性能不是很好,因为它将导致每个对象产生一个额外的数据库计数。
MPTT uses the
TreeManager
instance forget_children
which returns amodels.query.Queryset
, not your custom subclass of it. You chould do somethling like:Note that your
statements_with_count
method is not very performant, as it will cause one additional database count for each object.