How to show only first level child when second level child doen't exists otherwise show second level child in django MPTT -
i got following output django-mptt tree:
group-2 ministry e division g division z ministry f division division j group-3 ministry p division x division y ministry q ministry r division u division v
but want show follows:
division g division z division division j division x division y ministry q division u division v
that means level 1 shows when level 2 not exists otherwise level 2 shows. tried using model.objects.filter(level=1) gives 1 level not both selectively. tried model.objects.filter(level__gt=0) gives both level not obey condition.
how can both level maintaining conditions?
edit:
{% block content %} {% load mptt_tags %} {% instance in genre.objects.all %} {% if instance.is_leaf_node %} {{ instance }} {% endif %} {% endfor %} {% endblock %}
views:
def show_genres(request): instances = genre.objects.filter(children__isnull=true) return render(request, 'genre/template.html', {'instances': instances})
urls:
url(r'^genres/$', show_genres, name="genre_list",)
model:
class genre(mpttmodel): name = models.charfield(max_length=50, unique=true) parent = treeforeignkey('self', null=true, blank=true, related_name='children', db_index=true) class mpttmeta: order_insertion_by=['name']
show leaf nodes
solution #1 - in template:
{% instance in model.objects.all %} {% if instance.is_leaf_node %} {{ instance }} {% endif %} {% endfor %}
solution #2 - in view:
instances = model.objects.filter(children__isnull=true) return render(request, 'template.html', {'instances': instances})
Comments
Post a Comment