Sunday, July 15, 2012

Re: Hierarchy Model



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

--
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.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate