views:

119

answers:

2

Question regarding how to setup dbase relationships (newbie, this may be trivial)

Followed the django tutorial (Poll, Choices); understood that 1 Poll has many Choice(s), therefore many Choice(s) point to a single Poll.

   class Poll(models.Model):
      question = models.CharField(max_length=200)
      ...

   class Choice(models.Model):
      poll = models.ForeignKey(Poll)
      ...

Question: I have a database with places, persons, etc. (mutiple tables). A subset of my tables have a similar field. I want a place to have 1+ phone_number(s). I want a person to have 1+ phone_number(s). I may want other tables to have 1+ phone_number(s).

If I followed the Poll/Choice approach, then my problem is indicated by the question marks shown below under PhoneNumber.

   class Person(models.Model):
      firstname = models.CharField(max_length=20)
      ...

   class Place(models.Model):
      description = models.CharField(max_length=200)
      ...

   class PhoneNumber(models.Model):
      ??? = models.ForeignKey(???)
      ...

I have considered using inheritance so that both Person and Place inherit from the same base class. But I may have other fields besides phone_number for which I have a similar situation, and which span a different subset of tables. E.g.

               phone_number(s)     comments
               ---------------     --------
Person         yes                 no
Place          yes                 yes
Contract       no                  yes
...

Any advice on how to correctly design these types of relationships would be greatly appreciated. Thank you.

+1  A: 

I think you're looking for Generic Relations. Also here

hasen j
thanks for the reply - I am reading the docs.
jd
cool, if it remains unclear to you, you can update your question and let us know where you're stumbling.
hasen j
hasen, i read the docs, used the generic relations approach, and it solved my problem. thanks again.
jd
A: 

check ContenType app, and specifically Generic Relations. basically, you store both the type and id of the person/place object on the PhoneNumber object.

Javier
as with the first answer, thank you for the reply - I am reading the ContentType documentation.
jd