Hierarchy Model

79 views
Skip to first unread message

Setiaman Lee

unread,
Jul 15, 2012, 2:02:39 PM7/15/12
to DjangoUserGroup
Hi,

I want to implement hierarchy data model which is quite common in Relational Data Model.

Let's say I have Employee model which has the relation to the boss which link to the employee model itself.
It will look like this:

class Employee(models.Model):
    empname = models.CharField(max_length=60)
    boss = models.ForeignKey(Employee)
    salary = models.integer()


Sample Data:

ID    empname      boss        salary
--------------------------------------------------
1      albert            null          10000
2      bert              1              5000
3      Chuck          1              5000
4      Donna          3              3000
5      Jack            3              2000
 
Albert is the root with Bert and Chuck under him and Chuck has Donna and Jack under him.

I got an error when I tried to sync to the database where Django telling me that the employee is not exist in the Foreign key part.
Is there any way to manipulate this situation?

Cheers,
Setiaman

Andre Terra

unread,
Jul 15, 2012, 4:27:43 PM7/15/12
to django...@googlegroups.com

-- Sent from my phone, please excuse any typos. --

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Andre Terra

unread,
Jul 15, 2012, 4:30:05 PM7/15/12
to django...@googlegroups.com

I recommend using django-mptt. I can't link to it cause I'm writing on my phone, but it's easy to find it on Google.

Also, your foreign key should target self rather than Employee, IIRC.

Cheers,
AT

-- Sent from my phone, please excuse any typos. --

On Jul 15, 2012 11:02 AM, "Setiaman Lee" <setiam...@gmail.com> wrote:
--

lacry...@gmail.com

unread,
Jul 15, 2012, 5:20:25 PM7/15/12
to django...@googlegroups.com

Put 'Employee' or if that fails, 'appname.Employee' with the quotes, as a string literal, this works for self-reference and reference loops.

-----Mensaje original-----
De: Andre Terra
Enviados: 15/07/2012 13:30:05
Asunto: Re: Hierarchy Model

yarko

unread,
Jul 15, 2012, 6:44:27 PM7/15/12
to django...@googlegroups.com


On Sunday, July 15, 2012 9:02:39 AM UTC-5, Setiaman wrote:
Hi,

I want to implement hierarchy data model which is quite common in Relational Data Model.

Let's say I have Employee model which has the relation to the boss which link to the employee model itself.
It will look like this:

class Employee(models.Model):
    empname = models.CharField(max_length=60)
    boss = models.ForeignKey(Employee)

Employee is in the middle of being defined when you make this reference, so
... try something like:

    boss = models.ForeignKey('Employee')   # delayed evaluation

or:

   boss = models.ForeignKey('self')  # I think this reads nicer

See https://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey
    salary = models.integer()


Sample Data:

ID    empname      boss        salary
--------------------------------------------------
1      albert            null          10000
2      bert              1              5000
3      Chuck          1              5000
4      Donna          3              3000
5      Jack            3              2000
 
Albert is the root with Bert and Chuck under him and Chuck has Donna and Jack under him.

I got an error when I tried to sync to the database where Django telling me that the employee is not exist in the Foreign key part.
Is there any way to manipulate this situation?

Cheers,
Setiaman


Regards,
Yarko

Setiaman Lee

unread,
Jul 16, 2012, 1:07:54 AM7/16/12
to django...@googlegroups.com

Hi All,

It works by blocking it with the single quote. :)
Thanks for the help.

Cheers,
Setiaman

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/d5YolTy8ZO8J.
Reply all
Reply to author
Forward
0 new messages