What is the parent
of the top of the hierarchy?
(Note: I think you may have problems with having default=0
unless you can guarantee that there will be an entry in this model with cost_element_id=0
, which isn’t going to happen in an AutoField
.)
A common solution is to allow null=True
for that parent field to indicate the root of the tree. Another possibility is to directly assign a value to cost_element_id
and use that same value as its parent. (You could set them both to 0, that should work.)
Side note: Depending upon the size and depth of the tree, this “minimal” method of defining a hierarchy is far from optimal. If this is anything more than a “trivial” tree, you’ll want to look at using something like treebeard
for manage this structure.
(See Some modelling advice - #12 by KenWhitesell for a more detailed discussion, among other conversations here.)