OneToOne? Inheritance? Another solution for nested model relationships?

47 views
Skip to first unread message

Daniele Procida

unread,
May 26, 2014, 5:33:57 PM5/26/14
to Django Users
I've an application that's been happily running for a few years, that does this:

class Person(Model):
# everyone's a Person

class Researcher(Model):
# a Researcher is Person who publishes research
person = models.OneToOneField(Person)

class Publication(Model):
author = models.ForeignKey(Researcher)


But this is no longer enough: now I also need to distinguish between Researchers who are research students and members of staff. Those who are students will need new fields such as "thesis_title" and "supervisors".

But, I will *still* need the Researcher class independently of the new ResearchStudent and ResearchStaff classes, because it's needed for Publication.author.

So now it might look something like this:

class Person(Model):
# everyone's a Person

class Researcher(Model):
# a Researcher is Person who publishes research
person = models.OneToOneField(Person)

class ResearchStaff(Model):
researcher = models.OneToOneField(Researcher)

class ResearchStudent(Model):
researcher = models.OneToOneField(Researcher)
supervisors = models.ManyToManyField(ResearchStaff)

class Publication(Model):
author = models.ForeignKey(Researcher)


How manageable is this going to be? Is there a better way of doing what I need to do, perhaps through inheritance?

Thanks,

Daniele

Leonardo Giordani

unread,
May 28, 2014, 4:36:04 AM5/28/14
to django...@googlegroups.com
Daniele,

I usually solve such issues with Inheritance. I feel comfortable with it because it lets me (in your example) to manage both ResearchStudent and ResearchStaff independently, while keeping the Researcher parent model available to deal with "global" queries and data interaction.

As always with inheritance, I suggest to think twice before implementing it, but in this case I'd say that it fits perfectly.


Regards,

Leo

Leonardo Giordani
Author of The Digital Cat
My profile on About.me - My GitHub page - My Coderwall profile



--
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.
To post to this group, send email to 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/20140526213320.1042853662%40mail.wservices.ch.
For more options, visit https://groups.google.com/d/optout.

Daniele Procida

unread,
May 28, 2014, 8:38:18 AM5/28/14
to django...@googlegroups.com
On Wed, May 28, 2014, Leonardo Giordani <giordani...@gmail.com> wrote:

>I usually solve such issues with Inheritance. I feel comfortable with it
>because it lets me (in your example) to manage both ResearchStudent and
>ResearchStaff independently, while keeping the Researcher parent model
>available to deal with "global" queries and data interaction.

It it were a case where abstract inheritance would work, I would agree. Unfortunately, the Researcher model can't be abstract (because it has its own relations with Publications).

If I use multi-table inheritance, that solves part of the problem. However I don't know how well it would work if I have a Researcher, who at some point needs to be a ResearchStudent and maybe later becomes ResearchStaff.

Regards,

Daniele

Leonardo Giordani

unread,
May 30, 2014, 3:56:51 AM5/30/14
to django...@googlegroups.com
I see your point. In this case I'd recommend giving Researcher two foreign keys, one towards a Student model and one towards a Staff one, then add some logic to the model to easily check the actual specialization or to convert it among them. Basically you get a Researcher that "owns" a Student or a Staff role.

Does this fit better? Let me know

Regards,

Leo

Leonardo Giordani
Author of The Digital Cat
My profile on About.me - My GitHub page - My Coderwall profile



Daniele

--
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.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Rafael E. Ferrero

unread,
May 30, 2014, 7:53:49 AM5/30/14
to django...@googlegroups.com
2014-05-26 18:33 GMT-03:00 Daniele Procida <dan...@vurt.org>:
I've an application that's been happily running for a few years, that does this:

class Person(Model):
   # everyone's a Person

class Researcher(Model):
    # a Researcher is Person who publishes research
    person = models.OneToOneField(Person)

class Publication(Model):
    author = models.ForeignKey(Researcher)


But this is no longer enough: now I also need to distinguish between Researchers who are research students and members of staff. Those who are students will need new fields such as "thesis_title" and "supervisors".

But, I will *still* need the Researcher class independently of the new ResearchStudent and ResearchStaff classes, because it's needed for Publication.author.

So now it might look something like this:

class Person(Model):
   # everyone's a Person

class Researcher(Model):
    # a Researcher is Person who publishes research
    person = models.OneToOneField(Person)

class ResearchStaff(Model):
   researcher = models.OneToOneField(Researcher)

class ResearchStudent(Model):
   researcher = models.OneToOneField(Researcher)
   supervisors = models.ManyToManyField(ResearchStaff)
So, a ResearchStudent must be a Researcher and can have multiple supervisors, but every supervisor must to be a ResearchStaff and a Researcher too; and everyone is a Person here. Right?
Can a ResearchStudent be a ResearchStaff too?
i'll do something like this:






class Publication(Model):
    author = models.ForeignKey(Researcher)


How manageable is this going to be? Is there a better way of doing what I need to do, perhaps through inheritance?

Thanks,

Daniele
--
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.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages