我有一个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,
)
1条答案
按热度按时间lyr7nygr1#
我相信你想要的叫做
abstract class
:https://docs.djangoproject.com/en/4.1/topics/db/models/#abstract-base-classes这将在Django中创建这个抽象概念,但在后端仍然将它们创建为独立的表。您可以通过查看
python manage.py makemigrations
之后的迁移文件本身来测试这是否正确