对于扩展的信息,我应该使用Django的多表继承或显式的OneToOneField [英] For extended information, should I use Django's Multi-table Inheritance or an explicit OneToOneField

查看:222
本文介绍了对于扩展的信息,我应该使用Django的多表继承或显式的OneToOneField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 模型#1 :密集表包含经常搜索和始终填写的字段

  2. 模型#2 :包含不常搜索但不总是填写字段的稀疏表

  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. 我正在思考将其分成两个模型/表? li>
  2. 我应该使用Django的多表继承,还是应该明确定义一个 OneToOneField ?为什么?

  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?



配置




  • Django版本1.3.1

  • 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



    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)
    


    推荐答案

    你在这里主要处理苹果和苹果。 Django的MTI实现(多表继承)使用隐式的OneToOneField。

    You're essentially dealing with apples and apples here. Django's implementation of MTI (multi-table inheritance) uses an implicit OneToOneField.

    这篇关于对于扩展的信息,我应该使用Django的多表继承或显式的OneToOneField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆