【问题标题】:Django models kind of multi-table inheritance not workingDjango模型的多表继承不起作用
【发布时间】:2014-10-08 23:28:05
【问题描述】:

我想按照 Elmasri 和 Navathe 的“数据库系统基础”中所述对 DB 设计进行层次结构。

这意味着当我有一些为许多类/表共享的信息时,我可以将它放在主父表中,并使用主表 id 作为子表中的外键,这是一种弱实体。

我尝试使用抽象和多表继承(最后一个不允许我指定 OneToOneField,不知道在 django 文档中的哪里可以找到它)。

我的例子就在这里(每个班级一张桌子):

'''I would like this to be abstract, because I will never instantiate it, 
but could be not if needed'''

class Person(models.Model): 
    personId = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=45)
    surname = models.CharField(max_length=45, blank=True)
    email = models.CharField(max_length=45, blank=True)
    phone = models.CharField(max_length=15, blank=True)

    class Meta:
        managed = False
        db_table = 'person'

class Alumn(Person):
    # Maybe this one down should be OneToOne.
    # alumnId == personId always true for the same real world guy
    alumnId = models.ForeignKey('Person', db_column='alumnId', primary_key=True) 

    comments = models.CharField(max_length=255, blank=True)

class Meta:
    managed = False
    db_table = 'alumn'

# There are more child classes (Client, Professor, etc....) 
# but for the example this is enough

我的目标是在 DB 中创建一个 Alumn,只需要两句话:

a = Alumn(personId=1,name='Joe', [...more params...] , alumnId=1, comments='Some comments' )
a.save()

让这两行插入两行:一行用于 Person,另一行用于 Alumn。此处 sn-p up 中的 alumnId 属性可以省略,因为它始终与 personId 相同(我告诉过你,就像一个弱实体)。

我是 django 的初学者,但我查看了文档并用 abstract=True in Person 证明了一些事情,但没有成功我想现在我应该弄乱 init 构造函数用于构建超类,然后构建子类。

我不知道选择的正确路径,但绝对不想改变数据库设计。请帮忙。

提前致谢。

【问题讨论】:

    标签: python django django-models django-orm django-inheritance


    【解决方案1】:

    您的模型中不需要有 id; Django 自动处理它。另外,您不应该使用骆驼案。换句话说:personId 应该是 person_id 并且无论如何都不是必需的 - 只需将其删除即可。

    一般来说,我避免使用 ORM 进行非抽象继承。

    我不太了解您想要实现的目标,但我会根据您的需要建议 2 种方法(适用于 Person、Alumni、Professor 等):

    1.抽象继承:

    class Person:
        class Meta:
            abstract = True
    
        # here you put all the common columns
    

    然后:

    class Alumni(Person):
        # the other columns - specific to alumn
    

    等等

    通过这样做,您可以为每个人的子类型创建一个表格:校友、教授等。

    2。使用组合:

    class Alumn:
         person = models.ForeignKey(Person, null=True, related_name="alumni_at")
         university = ...
    
    class Professor:
         person = models.ForeignKey(Person, null=True, related_name="professor_at")
         university = ...
    

    这样你就可以做到:

    bob = Person.objects.create(first_name="bob", ...)
    Alumn.objects.create(person=bob, university="univ 1")
    Professor.objects.create(person=bob, university="univ 2")
    Alumn.objects.create(person=bob, university="univ 2")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-13
      • 2011-03-11
      • 2021-12-10
      • 2021-01-10
      • 1970-01-01
      • 2012-09-20
      • 2012-04-20
      • 2011-12-31
      相关资源
      最近更新 更多