【问题标题】:Django DB Model, Create two identical tables, one inheriting from the otherDjango DB 模型,创建两个相同的表,一个从另一个继承
【发布时间】:2018-11-24 13:21:29
【问题描述】:

我有一个要求,我想在 Django 中拥有相同的表。例如 MyTableCurrent 和 MyTableArchive。

除了类/表名和外键引用之外,这些表应该是相同的。

有没有办法基本上维护 MyTableCurrent 上的架构,然后在 MyTableArchive 下,我继承自 MyTableCurrent 的基类,但只覆盖外键字段以匹配相应的存档表?

【问题讨论】:

    标签: django django-models


    【解决方案1】:

    我找到了我正在寻找的解决方案。

    https://docs.djangoproject.com/en/2.1/topics/db/models/#abstract-base-classes

    class ServiceProviderBase(models.Model):
        sp_id = models.CharField(max_length=64, primary_key=True)
        name = models.CharField(max_length=64, null=True, blank=True)
        isEnterprise = models.CharField(max_length=5)
        cluster = models.CharField(max_length=64)
        created_date = models.DateTimeField(auto_now_add=True)
    
        class Meta:
            abstract = True
    
    
    class ServiceProviderCurrent(ServiceProviderBase):
        pass
    
    
    class ServiceProviderArchive(ServiceProviderBase):
        pass
    
    
    class GroupBase(models.Model):
        id = models.AutoField(primary_key=True)
        grp_id = models.CharField(max_length=64)
        name = models.CharField(max_length=64, null=True, blank=False)
        userLimit = models.IntegerField()
        organizationType = models.CharField(max_length=64)
        created_date = models.DateTimeField(auto_now_add=True)
    
        class Meta:
            abstract = True
    
    
    class GroupCurrent(GroupBase):
        organizationId = models.ForeignKey('sandbox.ServiceProviderCurrent', on_delete=models.CASCADE)
    
    
    class GroupArchive(GroupBase):
        organizationId = models.ForeignKey('sandbox.ServiceProviderArchive', on_delete=models.CASCADE)
    

    【讨论】:

      【解决方案2】:

      您可以创建一个表和从该表继承的其他两个表。示例:

      class BaseMedal(models.Model):
        field_one = models.ForeignKey(TableName,...)
        field_two = models.ForeignKey(AnotherTableName,...)
      
      class ModelOne(BaseModal)
        field = models.TextField(...)
      
      class ModelTwo(BaseModel)
        field = models.TextField(...)
      

      这样,ModelOne 和 ModelTwo 的外键字段是相同的。

      【讨论】:

        【解决方案3】:

        您可以使用Proxy Model。例如:

        class BaseModel(models.Model):
            # your common fields
        
        
        class Current(BaseModel):
             class Meta:
                 proxy=True
        
             def some_current_model_method(self):
                 # specific method for this class
        
        class Archive(BaseModel):
             class Meta:
                 proxy=True
        
             def some_archive_model_method(self):
                 # specific method for this class
        

        更新

        用法:

        archive1 = Archive.objects.create(title="Archive 1")
        archive2 = Archive.objects.create(title="Archive 2")
        current1 = Current.objects.create(title="Current 1")
        current2 = Current.objects.create(title="Current 2")
        
        
        archives = Archive.objects.all().count()  # will print 4
        currents = Current.objects.all().count()  # will print 4
        base = BaseModel.objects.all().count()  # print 4
        
        
        Archive.objects.get(title="Archive 1")  # will work
        Archive.objects.get(title="Current 1")  # will work
        
        Current.objects.get(title="Archive 1")  # will work
        Current.objects.get(title="Current 1")  # will work
        
        BaseModel.objects.get(title="Archive 1")  # will work
        BaseModel.objects.get(title="Current 1")  # will work
        
        BaseModel.objects.create(title="BaseModel")
        Archive.objects.get(title="BaseModel")  # will work
        Current.objects.get(title="BaseModel")  # will work
        

        【讨论】:

        • 寻找代理文档,它有以下评论。 'MyPerson 类在与其父 Person 类相同的数据库表上运行。特别是,任何新的 Person 实例也可以通过 MyPerson 访问,反之亦然。如果我的 current 和 archive 类对于同一个表字段有两个不同的值,但是在 current 或 archive 中,这是如何工作的?
        • 感谢您的详细解释,但我仍然认为这不会达到我的预期目的。例如,在前四行,假设 archive1 和 current1 但标题为“我的标题”。当前表和存档表的主键很可能是相同的值。我需要一个允许我在两个表中具有相同值的解决方案。我试图避免的主要部分是在两个模型上维护模式,我关心的唯一区别是表名和外键匹配适当的表。
        • @JeffA 抱歉,我没有正确测试我的代码。我在上一个答案中给出了错误的例子。请查看我的更新答案。
        猜你喜欢
        • 2021-06-18
        • 1970-01-01
        • 2020-03-16
        • 2017-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多