对于扩展信息,我应该使用 Django 的多表继承还是显式的 OneToOneField
我的模型上有一些字段并不总是会被填写(例如,项目结束之前的实际完成日期和成本)。因此,我想将模型分为两个:
- 模型#1:包含经常搜索且始终完成的字段的密集表
- 模型#2:包含不频繁搜索的稀疏表搜索但并不总是完成的字段
问题
- 我将其分为两个模型/表的想法是否正确?
- 我应该使用 Django 的多表继承,还是应该显式定义
OneToOneField
?为什么?
配置
- Django 版本 1.3.1
模型
使用 Django 的多表继承
class Project(models.Model):
project_number = models.SlugField(max_length=5, blank=False,
primary_key=True)
budgeted_costs = models.DecimalField(max_digits=10, decimal_places=2)
class ProjectExtendedInformation(Project):
submitted_on = models.DateField(auto_now_add=True)
actual_completion_date = models.DateField(blank=True, null=True)
actual_project_costs = models.DecimalField(max_digits=10, decimal_places=2,
blank=True, null=True)
使用显式 OneToOneField
class Project(models.Model):
project_number = models.SlugField(max_length=5, blank=False,
primary_key=True)
budgeted_costs = models.DecimalField(max_digits=10, decimal_places=2)
class ProjectExtendedInformation(models.Model):
project = models.OneToOneField(CapExProject, primary_key=True)
submitted_on = models.DateField(auto_now_add=True)
actual_completion_date = models.DateField(blank=True, null=True)
actual_project_costs = models.DecimalField(max_digits=10, decimal_places=2,
blank=True, null=True)
I have some fields on a model that won't always be filled in (e.g., the actual completion date and costs until the project is over). Because of this, I thought I'd split the model into two:
- Model #1: Dense table containing frequently searched and always completed fields
- Model #2: Sparse table containing infrequently searched and not always completed fields
Questions
- Am I thinking correctly in splitting this into two models/tables?
- Should I use Django's Multi-table Inheritance, or should I explicitly define a
OneToOneField
? Why?
Configuration
- Django version 1.3.1
Models
Using Django's Multi-table Inheritance
class Project(models.Model):
project_number = models.SlugField(max_length=5, blank=False,
primary_key=True)
budgeted_costs = models.DecimalField(max_digits=10, decimal_places=2)
class ProjectExtendedInformation(Project):
submitted_on = models.DateField(auto_now_add=True)
actual_completion_date = models.DateField(blank=True, null=True)
actual_project_costs = models.DecimalField(max_digits=10, decimal_places=2,
blank=True, null=True)
Using an Explicit OneToOneField
class Project(models.Model):
project_number = models.SlugField(max_length=5, blank=False,
primary_key=True)
budgeted_costs = models.DecimalField(max_digits=10, decimal_places=2)
class ProjectExtendedInformation(models.Model):
project = models.OneToOneField(CapExProject, primary_key=True)
submitted_on = models.DateField(auto_now_add=True)
actual_completion_date = models.DateField(blank=True, null=True)
actual_project_costs = models.DecimalField(max_digits=10, decimal_places=2,
blank=True, null=True)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你在这里本质上是在处理苹果和苹果。 Django 的 MTI(多表继承)实现使用隐式 OneToOneField。
You're essentially dealing with apples and apples here. Django's implementation of MTI (multi-table inheritance) uses an implicit OneToOneField.