对于扩展信息,我应该使用 Django 的多表继承还是显式的 OneToOneField

发布于 2024-12-05 22:53:07 字数 1518 浏览 1 评论 0原文

我的模型上有一些字段并不总是会被填写(例如,项目结束之前的实际完成日期和成本)。因此,我想将模型分为两个:

  1. 模型#1:包含经常搜索且始终完成的字段的密集表
  2. 模型#2:包含不频繁搜索的稀疏表搜索但并不总是完成的字段

问题

  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:

  1. Model #1: Dense table containing frequently searched and always completed fields
  2. Model #2: Sparse table containing infrequently searched and not always completed fields

Questions

  1. Am I thinking correctly in splitting this into two models/tables?
  2. 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 技术交流群。

扫码二维码加入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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文