Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8490863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:14:58+00:00 2026-06-10T22:14:58+00:00

as a very much of a newbie in Django/python world I fail to find

  • 0

as a very much of a newbie in Django/python world I fail to find a way to check whether an object has children.

An example:

Class MyItems
        title = models.CharField(max_length=50)
        parent = models.ForeignKey('self',null=True, blank=True,related_name='subitems')

Then in my template:

{% for item in MyItems %}
<li> {{ item.title }} </li>
    {% if item **IS A PARENT OF CHILDREN** %}
        <p>This is what I want</p>
    {% endif %}
{% endfor %}  

I can see if an item has a parent no problem, but how to do it other way around, tell if an item is a parent to other item?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:14:59+00:00Added an answer on June 10, 2026 at 10:14 pm

    if you want a recursive parent child relationship between your objects you should look at using MPTT

    http://django-mptt.github.com/django-mptt/

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

    talked about in the cookbook here:
    https://code.djangoproject.com/wiki/ModifiedPreorderTreeTraversal

    to understand how MPTT works at a data level, have a look at http://en.wikipedia.org/wiki/Nested_set_model

    The problem with the obvious solution, is that for each additional level children, another query is required – which gets extremely inefficient.

    # this is an additional query AND will not be recursive.
    {% if item.child_set.all.count > 0 %} 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very much a newbie when it comes to all this so forgive my
I have a C# background. I am very much a newbie to a low-level
Very much a newbie with F#, can't seem to get my googler to work
I'm very much newbie to VS Addins. Although, I subscribed to DocumentEvent.DocumentOpened. But additionally,
I am a newbie (very much so), so my question might be dumb, but
I am very much an Android newbie and I have built a simple RSS
I'm very much a Tomcat newbie, so I'm guessing that the answer to this
I am a newbie with css coding (which very much shows) - can anybody
Very much related to my previous question , but I found this to be
Would very much appreciate any help or hint on were to go next. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.