3

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?

nukl
  • 10,073
  • 15
  • 42
  • 58
  • Did you try list(Thing.objects.all())[5:10] - that might work, since Django has to fetch all data before slicing. Your original call uses a LIMIT and OFFSET clause in your SQL.You might do that in a python shell and see if it works. – mawimawi Dec 14 '11 at 09:19

1 Answers1

2

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 call recursetree 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:

nodes = [node.get_descendants(include_self=True) 
         for node in Thing.objects.all()[5:10]]

And in the template:

{% for node in nodes %}
    {% recursetree node %}...{% endrecursetree %}
{% endfor %}
Filip Dupanović
  • 32,650
  • 13
  • 84
  • 114