Django DB模型,创建两个相同的表,一个从另一个表继承

时间:2018-11-24 13:21:29

标签: django django-models

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

除了类/表名和外键引用外,其他表应相同。

是否有一种方法可以基本维护MyTableCurrent上的架构,然后在MyTableArchive下,我继承自MyTableCurrent的基类,但仅覆盖外键字段以匹配相应的存档表?

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

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

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的外键字段相同。

答案 2 :(得分:0)

您可以使用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