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 8062021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:31:37+00:00 2026-06-05T10:31:37+00:00

I am trying to model a tree structure within django. This is a reduced

  • 0

I am trying to model a tree structure within django. This is a reduced version of what I’ve got:

from django.db import models

class Node(models.Model):
    parent = models.ForeignKey("Node", null=True)
    name = models.CharField(max_length=20)

    def child_cnt(self):
        return self.node_set.count()

    def __unicode__(self):
        return self.name

So far so good. It works. But if I now start to create a hierarchy like that:

from ....models import Node
root = Node()
root.name = "ROOT"
root.parent = None
root.save()

n = Node()
print n.child_cnt()
>> 1
print n.node_set.all()
[<Node: ROOT>]

So what does the root node make as a child inside n? And how can I avoid that?

The problem disappears once I called n.save() but it is kind of nasty to see a node initialized with a child_cnt of 1 inside the admin site.

  • 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-05T10:31:39+00:00Added an answer on June 5, 2026 at 10:31 am

    I know that sounds crazy, but I spent a few minutes trying to solve your problem and It didnt work in any attempt that I tried.
    Anyways, for the Tree ForeignKey, I find another solution Online, which is a entire library that takes care of this type of Key, https://github.com/django-mptt/django-mptt
    You can try it out, it might fit for you. I hate having to add a library for such a small thing, however I could not find anything better.

    Also if you dont wanna import the entire lib, you can kind of bring just the TreeForeignKey to your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to define a tree structure in GORM. Here is my model: class
I'm trying to implement a tree like structure using a Materialized Path model described
I am trying to model a tree relationship in a table. For instance, there
I'm trying to model class and sections of a class and further instances of
I am trying to model this relationship following this link http://www.javaworld.com/javaworld/jw-01-2008/images/datamodel.gif Its the usual
i am trying to validate a field depending on attributes from the associated model.
I'm trying to structure my app in Python. Coming back from C#/Java background, I
I'm stuck trying to retrieve data from an associated model store. Allow me to
I'm using Materialized Path tree (provided by django-treebeard) to create a tree structure (
Relatively new to rails and trying to model a very simple family tree with

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.