mptt树分页
我想对 mpttmodel 实例进行简单的分页。我有这个模型:
class Thing(MPTTModel):
text = models.TextField()
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
问题是,当我尝试检索具有偏移量的对象时,例如:
Thing.objects.all()[5:10]
{% recursetree things %}
模板标记引发异常: 渲染时捕获断言错误:无法重新排序查询一旦切片被取出。
如何解决这个问题?
I want to make simple pagination of mpttmodel instances. I have this model:
class Thing(MPTTModel):
text = models.TextField()
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
The problem is, when i trying to retrive objects with offset, like:
Thing.objects.all()[5:10]
{% recursetree things %}
template tag raises exception: Caught AssertionError while rendering: Cannot reorder a query once a slice has been taken.
How to solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
recursetree
标记需要传递给它的查询集,该查询集没有应用数组切片限制语法。您可以使用模型和管理器实例来构建更合适的查询集或者调用
recursetree
并遍历节点,过滤掉它们,如果需要的话,再次使用所选节点从其中调用recursetree
,但这有点复杂。现在,看起来您可以通过以下方式实现您想要的:
在模板中:
The
recursetree
tag needs a queryset passed to it that doesn't have the array-slicing limit syntax applied to it.You can either use the model and manager instances to construct a more suitable queryset or call
recursetree
and traverse the nodes, filter them out and callrecursetree
from therein with the selected nodes again if you need to, but that's a bit more convoluted.Right now, looks like you could achieve what you want with:
And in the template: