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+unsubscr...@googlegroups.com
<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to django-users@googlegroups.com
<mailto:django-users@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.

--
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/532634F3.1080300%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

Reply via email to