Recursive one-to-many foreign key on the same table

102 views
Skip to first unread message

Phlip Pretorius

unread,
Aug 9, 2018, 8:42:13 AM8/9/18
to Django users
I want to build a one-to-many recursive foreign key on a table in Django.

The concept is something like this:

Table: staff/person

staff number    Surname            Firstnames    Supervisor
4637            Zondo            Julien       
859076            Vanderbildt        John        4637

That is, Julien Zondo is John Vanderbildt's supervisor and to enter the staff number in the supervisor field, it must exist in the staff table.

Each staff member can have only one supervisor but a person can be the supervisor of many staff members.

I have read the documentation at https://docs.djangoproject.com/en/2.1/ref/models/fields/#recursive-relationships but i do not get the gist of it

I want to build an index table on three levels where a level 1 index will have no predecessor
Level 2 indexes must have a level 1 predecessor and a level 3 index must have a level 2 pedecessor.

Can this be done by using recursive relationships or should this be done in a business layer?

Here is part of the definition of the model:
=============================================================================
Class DecisionIndex(models.Model):
    """A mechanism to index decision taken by a committee"""
    IndexCode = models.CharField(max_length=6)
    IndexDesc = models.CharField(max_length=80, null=False)
    IndexLevel = models.IntegerField(choices=list(zip(range(1, 4),
                                                      range(1, 4))),
                                     null=False)
    IndexPredecessor = models.ForeignKey('self',
                                         on_delete=models.CASCADE,
                                         limit_choices_to={'IdexLevel': 1},)
============================================================================

I get the following error message:
no such column: komadm_apps_decisionindex.IndexPredecessor_idno such column:

Any help will be welcome.

Regards,

Phlip

Marius Räsener

unread,
Aug 9, 2018, 10:00:05 AM8/9/18
to Django users
Hey Philip,

while I can't help you with your actual implementation I wanted to link you to this presentation abuot the topic, it's worth time spend I think ...


recursive models are tricky, especially to query afterwards - your example might be not super complicated (yet) but still, I recommend the video...

Marius

Jason

unread,
Aug 9, 2018, 11:11:54 AM8/9/18
to Django users
i would look at https://github.com/django-mptt/django-mptt and see if that fits your needs.  Sounds like you want to implement a limited level tree structure in a db table.  If so, this package is a very good one.

Tosin Ayoola

unread,
Aug 9, 2018, 11:31:56 AM8/9/18
to django...@googlegroups.com
thanks for the response but can you help me go through the code probably i'm the person making mistake in my code this the github link: https://github.com/olaneat/ask_the_schools

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bc512a99-625d-4ac1-8a2d-0ffd4a5692be%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Phlip Pretorius

unread,
Aug 10, 2018, 8:00:05 AM8/10/18
to Django users
Thank you Marius; the video is worth the watch and thought provoking

Phlip
Reply all
Reply to author
Forward
0 new messages