Django:设计具有不同领域的模型的建议 [英] Django: Advice on designing a model with varying fields

查看:125
本文介绍了Django:设计具有不同领域的模型的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些建议/意见,最好的方法是在django中创建一个动态模型。



结构需要描述产品的数据。有大约60种不同的可能的数据点可能是相关的,每个产品根据其产品类型选择这些点中的约20个(重叠很多)。有大约二打不同的产品类型,每种产品类型分为6类。



明显的,但不可思议的方法是将所有100个数据点放入产品型号,然后选择并忽略任何留空的字段。优点:很明显缺点:它是丑陋的,如果我需要添加更多的数据点,它需要更新数据库。



我的想法是使每个产品完全抽象,只定义产品类型和其他一些识别特征。

  class Product(models.Model):
productType = models然后我将为每种数据点分别设一个模型(数字,数字,数字,文本,日期等),如下所示:

  class TextData(models.Model):
fieldName = models.CharField(etc)
verboseName = models.CharField(etc)
data = models.CharField(etc)
product = models.ForeignKey(Product)

最后,我将制作一个模型,定义与每个产品类型相关的数据类型,以及每个数据点应该是什么

  class ProductType(models.Model):
name = models.CharF ield(etc)
....

所以主要的问题是:填写产品类型的相关领域的最佳方法,当它们可以变化时?也许另一个模型保存数据点的名称/类型,以及ProductType和该模型之间的ManyToManyField?带XML的文本字段?定义结构的外部XML字段?放弃这整个追求,再去做些更好的事情?



感谢任何帮助!

解决方案

如果您正在寻找在django中的某些类型的 eav style ,看看 eav-django django-expando


I'm looking for some advice/opinions on the best way to approach creating a sort-of-dynamic model in django.

The structure needs to describe data for Products. There are about 60 different possible data points that could be relevant, with each Product choosing about 20 of those points (with much overlapping) depending on its ProductType. There are about 2 dozen different ProductTypes, with each ProductType falling into one of 6 categories.

The obvious, but ungraceful method, would be to just put all 100 data points into the Product model, and just pick and choose, and ignore any fields left empty. Advantage: it's obvious. Disadvantage: it's ugly, and if I need to add more data points, it requires updating the database.

My thought is to make each product completely abstract, and only define its product-type, and a few other identifying traits.

class Product(models.Model):
    productType = models.ForeignKey(ProductType)

I would then have a separate models for each kind of data point (number, text, date, etc.) which would look like this:

class TextData(models.Model):
    fieldName = models.CharField(etc)
    verboseName = models.CharField(etc)
    data = models.CharField(etc)
    product = models.ForeignKey(Product)

Finally, I would make a model that defines what kinds of data are relevant for each product-type, and what each data point should be called.

class ProductType(models.Model):
    name = models.CharField(etc)
    ....

So the main question is: what would be the best way to fill out the relevant fields for a product type, when they can be varying? Perhaps another model that holds the name/type of a data point, and a ManyToManyField between ProductType and that model? A text field with XML? An outside XML field defining the structure? Abandon this entire pursuit and go with something better?

Thanks for any help!

解决方案

If you are looking for implementations of dynamic attributes for models in django in some kind of eav style, have a look at eav-django, or at django-expando.

这篇关于Django:设计具有不同领域的模型的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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