【问题标题】:Field with inner structure in DjangoDjango中具有内部结构的字段
【发布时间】:2013-09-09 20:56:21
【问题描述】:

我想在Django 中创建一个项目表。问题是每个 项目有不同的内部结构,我必须创建太多的表。

例如,如果项目是CPU,我希望内部结构属性(列)如下:

type:processor
frequency:value
number of cores:value
socket:value

如果项目为smartphone,则内部结构为:

type:smartphone
os:value
displaysize:value
cpuID:value

当向数据库添加新项目时,该字段应首先询问用户他想要添加什么类型(例如 CPU)并根据此信息显示适当的表格 对于依赖于类型的内部结构属性。内部结构的某些字段是必需的,但大部分是可选的,并且有些字段可以是多个(例如多个 URL)。

实现这一点的方法是什么? Django 中是否已经有一些帮助 这样的数据?各种方法的优缺点是什么? 到目前为止,我看到了一个缺点。请注意,智能手机使用 cpuID 作为外键。是否可以保留参考文献?请注意,CPU 可以引用它的制造商(只是一个示例,各种项目可以有一些其他项目的引用)。

【问题讨论】:

    标签: django database-design django-models data-modeling database-schema


    【解决方案1】:

    一种方法是创建一个包含所有公共属性的“主”表,并为每种类型的对象创建单独的表。这在 Django 中很容易做到,模型定义看起来很“干净”,详情请参阅Multi-table inheritance

    适合您情况的示例模型:

    # Hold common fields/properties
    class Item(models.Model):
        type = ...
        price = ...
        weight = ...
        width = ...
        height = ...
        ...
    
    # Below are example classes which will inherit all properties from Item
    class CPU(Item):
        frequency = ...
        core_count = ...
        socket = ...
    
    class Smartphone(Item):
        os = ...
        display_size = ...
        cpu = models.ForeignKey(CPU, ...)  # Example linking between items.
    

    请注意,每个“具体”项目由两个数据库行组成:公共表和“具体”表。这两个表通过“具体”表中的一对一字段连接起来(Django 会为您添加此字段,但您可以根据需要重新定义它)。

    从数据库中检索项目的示例方法:

    # Get all "base" items without touching type tables
    Item.objects.all()
    
    # Get all items along with their child properties. This is rather ugly and expensive.
    Item.objects.select_related('cpu', 'smarthphone', ...).all()
    
    # Gets smartphones, uses a join to retrieve data from two tables.
    # I don't remeber if the `select_related` is required here.
    Smartphone.objects.select_related('item').all()  
    
    # When type of the item is only know at runtime, you can query like this (requires additional table column for storing type):
    Item.objects.filter(type='smartphone')
    # Or alternatively finding out which type class you want.
    

    优点:

    1. 类定义看起来简洁明了。
    2. 非常接近最佳数据库结构。
    3. 一个数据库查询可以检索各种类型的项目。

    缺点:

    1. 检索包含完整数据的对象时连接过多。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      相关资源
      最近更新 更多