1

关于如何设置数据库关系的问题(新手,这可能是微不足道的)

遵循 django 教程(投票,选择);理解 1 Poll 有很多 Choice(s),因此许多 Choice(s) 指向一个 Poll。

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

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

问题:我有一个包含地点、人员等的数据库(多表)。我的表的一个子集有一个类似的字段。我想要一个有 1 个以上电话号码的地方。我希望一个人拥有 1 个以上的电话号码。我可能希望其他表有 1+ 个电话号码。

如果我遵循 Poll/Choice 方法,那么我的问题由下面 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(???)
      ...

我考虑过使用继承,以便 Person 和 Place 都继承自同一个基类。但除了 phone_number 之外,我可能还有其他字段,我有类似的情况,并且跨越不同的表子集。例如

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

任何关于如何正确设计这些类型的关系的建议将不胜感激。谢谢你。

4

2 回答 2

1

我认为您正在寻找Generic Relations也在这里

于 2009-02-11T16:50:45.083 回答
0

检查ContenType应用程序,特别是Generic Relations。基本上,您将 person/place 对象的类型和 id 都存储在 PhoneNumber 对象上。

于 2009-02-11T16:52:18.027 回答