0

I have added a custom field for my model (status). But I want to use a custom query for children:

my template tag is:

def get_top_menu(context):    
    item = Item.objects.all()
    try:
       item = Item.objects.filter(position__position='top')
    except ObjectDoesNotExist:
       item = Item.objects.none()
   return {
      'nodes': item,         
   }

and template:

<ul class="root">
{% recursetree nodes %}
    <li>
        {{ node.name }}
        {% if not node.is_leaf_node %}
            <ul class="children">
                {{ children }}
            </ul>
        {% endif %}
    </li>
{% endrecursetree %}

as doc

How can I using a custom query for children?

halfer
  • 19,824
  • 17
  • 99
  • 186
TheNone
  • 5,684
  • 13
  • 57
  • 98

1 Answers1

0

Take a look at the MPTTModel instance methods docs (under Models and Managers section). There is a get_children() method which creates a QuerySet containing the immediate children of the model instance, in tree order. The benefit of using this method over the reverse relation provided by the ORM to the instance’s children is that a database query can be avoided in the case where the instance is a leaf node (it has no children).

inejc
  • 550
  • 3
  • 14