如何继承一个模型来使用它的字段,但在Django中又是一个独立的表?

tkqqtvp1  于 2023-03-20  发布在  Go
关注(0)|答案(1)|浏览(104)

我有一个CompanyRanking模型:

class CompanyRanking():
    """Company ranking model.

    Rankin hold a top based on minutes usage for user.
    """

    company = models.ForeignKey(
        'companies.company',
        on_delete=models.CASCADE,
        null=True,
        related_name='ranking'
    )

    user = models.ForeignKey(
        'users.user',
        on_delete=models.CASCADE,
        null=True,
        related_name='ranking_position'
    )

    extra_data = models.JSONField(
        null=True,
        blank=True
    )

我需要创建另一个模型,它包含与CompanyRanking相同的信息,但与另一个表(CompanyBatchByDate)有额外的关系,我在该表中获取日期以过滤用户的点数并显示其他排名。

class CompanyRankingByBatch(CompanyRanking):
    """
    Company ranking model based
    on batch.
    """

    batch = models.ForeignKey(
        'companies.CompanyBatchByDate',
        on_delete=models.SET_NULL,
        null=True,
        related_name='ranking_by_batch',
    )

class CompanyBatchByDate():
    """
    Company ranking model based
    on batch.
    """

    company = models.ForeignKey(
        'companies.company',
        on_delete=models.CASCADE,
        null=True,
        related_name='batch_by_date'
    )

    initial_date = models.DateField(
        null=True,
        blank=True,
        help_text='Date when the batch start'
    )

    finish_date = models.DateField(
        null=True,
        blank=True,
        help_text='Date when the batch finish'
    )

    is_active = models.BooleanField(
        default=True,
        help_text='Batch that will be used to calculate the ranking'
    )

    name = models.CharField(
        max_length=255,
        null=True,
        blank=True,
        help_text='Name of the batch'
    )

问题是,我希望能够拥有user - companyrankingbybatch关系的不同related_name,并希望独立于CompanyRanking更新此模型字段。
我已经尝试过通过添加parent_link = False来删除由django创建的一对一关系,这是因为继承。

companyranking_ptr = models.OneToOneField(
        'companies.CompanyRanking',
        on_delete=models.SET_NULL,
        parent_link=False,
        primary_key=False,
        null=True,
    )
lyr7nygr

lyr7nygr1#

我相信你想要的叫做abstract class:https://docs.djangoproject.com/en/4.1/topics/db/models/#abstract-base-classes
这将在Django中创建这个抽象概念,但在后端仍然将它们创建为独立的表。您可以通过查看python manage.py makemigrations之后的迁移文件本身来测试这是否正确

class BaseCompany(models.Model):
    company = models.ForeignKey(
        'companies.company',
        on_delete=models.CASCADE,
        null=True,
        related_name='ranking'
    )
    user = models.ForeignKey('users.user', on_delete=models.CASCADE, null=True, related_name='ranking_position')
    extra_data = models.JSONField(null=True, blank=True)

    class Meta:
        abstract = True

class CompanyRanking(BaseCompany):
    """
    Default Company with default fields
    self.company related_name = ranking
    """
    pass

class CompanyBatchByDate(BaseCompany):
    """
    Default Company with changed related_name
    self.company related_name = batch_by_date
    """
    company = models.ForeignKey(
        'companies.company',
        on_delete=models.CASCADE,
        null=True,
        related_name='batch_by_date'
    )

相关问题