简体   繁体   English

Django:有关设计具有不同字段的模型的建议

[英]Django: Advice on designing a model with varying fields

I'm looking for some advice/opinions on the best way to approach creating a sort-of-dynamic model in django. 我正在寻找有关在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. 大约有60个可能相关的不同数据点,每个产品根据其ProductType选择其中的20个点(重叠很多)。 There are about 2 dozen different ProductTypes, with each ProductType falling into one of 6 categories. 大约有2种不同的ProductType,每种ProductType属于6个类别之一。

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. 一种明显但不友好的方法是将所有100个数据点都放入Product模型中,然后进行选择,然后忽略任何保留为空的字段。 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? 也许另一个拥有数据点名称/类型的模型,以及在ProductType和该模型之间的ManyToManyField? A text field with XML? 使用XML的文本字段? An outside XML field defining the structure? 定义结构的外部XML字段? Abandon this entire pursuit and go with something better? 放弃整个追求,选择更好的东西吗?

Thanks for any help! 谢谢你的帮助!

如果您正在以某种eav样式在django中寻找模型的动态属性的实现,请查看eav-djangodjango-expando

This is ordinary relational database design. 这是普通的关系数据库设计。 Don't over-optimize it with OO and inheritance techniques. 不要使用OO和继承技术对其进行过度优化。

You have a Product Category table with (probably) just names. 您有一个“产品类别”表,其中仅包含名称。

You have a Product Type table with not much information with FK's to category. 您有一个“产品类型”表,其中没有多少信息与FK一起分类。

You have a Product table with an FK relationship to Product Type. 您有一个与产品类型有FK关系的产品表。

You have a Product Feature table with your data points and an FK to product. 您有一个包含产品数据点和产品FK的产品功能表。

To work with a Product, you query the product. 要使用产品,请查询产品。 If you need the features, you're just working with the "feature_set". 如果您需要这些功能,则只需使用“ feature_set”即可。 Django fetches them for you. Django为您获取它们。

This works remarkably well and is a well-established feature of a relational database and an ORM layer. 这非常有效,并且是关系数据库和ORM层的既定功能。 The DB caching and the ORM caching make these queries go very quickly. 数据库缓存和ORM缓存使这些查询的执行非常迅速。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM