Is there a better way to do this?

79 views
Skip to first unread message

Gene Coetzee

unread,
Mar 16, 2014, 6:59:59 PM3/16/14
to django...@googlegroups.com
Hi There!

Starting out with Django, and was looking for a bit of guidance. I am trying to establish a Organizational structure for employee/supervisor type relationships. Since each of these individuals will be a user, I am extending trying to also take advantage of django's built-in contrib.auth package. 

In Short, employees report to supervisors, and in turn supervisors in themselves can report to their respective supervisors... I developed the following models, but was curious if there might be a better way to do this:

class Supervisor(models.Model):
supervisor = models.OneToOneField(User)
def __unicode__(self):
return self.supervisor.username

class Employee(models.Model):
employee = models.OneToOneField(User)
supervisor = models.ForeignKey(Supervisor)
def __unicode__(self):
return self.employee.username

Thank you!


Mike Dewhirst

unread,
Mar 16, 2014, 7:34:11 PM3/16/14
to django...@googlegroups.com
On 17/03/2014 9:59am, Gene Coetzee wrote:
> Hi There!
>
> Starting out with Django, and was looking for a bit of guidance. I am
> trying to establish a Organizational structure for employee/supervisor
> type relationships. Since each of these individuals will be a user, I am
> extending trying to also take advantage of django's built-in
> contrib.auth package.
>
> In Short, employees report to supervisors, and in turn supervisors in
> themselves can report to their respective supervisors... I developed the
> following models, but was curious if there might be a better way to do this:

There will be a few different opinions on this ... here's mine:

class Employee(models.Model):
user = models.ForeignKey(User)
def __unicode__(self):
return self.user.username

class Relationship(models.Model):
boss = models.ForeignKey(Employee)
worker = models.ForeignKey(Employee)
relationship_type = models.CharField(max_size=32)
def __unicode__(self):
return "{0} {1} {2}".format(boss, relationship_type, worker)

I just typed this without testing and I think you would need to read up
on 'related_name' and use null=True here and there to get it working.

I would choose this approach to get some flexibility with multiple
relationships between employees. Supervisors get supervised too. Also, I
might choose different field names than "boss" and "worker" if I was
doing this for myself.

Mike

>
> class Supervisor(models.Model):
> supervisor = models.OneToOneField(User)
> def __unicode__(self):
> return self.supervisor.username
>
> class Employee(models.Model):
> employee = models.OneToOneField(User)
> supervisor = models.ForeignKey(Supervisor)
> def __unicode__(self):
> return self.employee.username
>
> Thank you!
>
>
> --
> 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...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/3c6cac23-7218-4c14-8039-2764217f6aad%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/3c6cac23-7218-4c14-8039-2764217f6aad%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Gene Coetzee

unread,
Mar 16, 2014, 9:06:28 PM3/16/14
to django...@googlegroups.com
Very helpful, thanks Mike!

C. Kirby

unread,
Mar 17, 2014, 2:01:19 PM3/17/14
to django...@googlegroups.com
I would do it with a recursive ForeignKey


class Employee(models.Model):
    employee = models.OneToOneField(User)
    supervisor = models.ForeignKey('self', blank=True, null=True)

This keeps the information in one table and pretty clean looking.
Allows all employees to have a supervisor, but they don't have to have one (say, CEO)
If an employee may have more than one supervisor you can use a ManyToMany instead of ForeignKey

Gene Coetzee

unread,
Mar 17, 2014, 4:32:05 PM3/17/14
to django...@googlegroups.com
Thank you C. Kirby. This is great, and will work very well for what I am trying to accomplish. Thanks again.
Reply all
Reply to author
Forward
0 new messages