Wednesday, May 28, 2014

Re: OneToOne? Inheritance? Another solution for nested model relationships?

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


2014-05-26 23:33 GMT+02:00 Daniele Procida <daniele@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)

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+unsubscribe@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/20140526213320.1042853662%40mail.wservices.ch.
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+unsubscribe@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/CAEhE%2BOkbUadKLYPqjqqLFAkdrBY86Lyjv_AoYG%3Dd8fY6A6WdnQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment