Django模型配置的动态属性

时间:2018-12-16 17:46:08

标签: django database python-3.x django-models

我一直在尝试找出在Django中为我们的PIM / PriceModel应用设置模型的最佳方法(或正确方法)。

示例模型(已剥离):

class ProductItem(models.Model):
    """
    An actual item/product, what it all revolves around.
    """
    part_number = models.CharField(unique=True, max_length=50, help_text='')
    internal_part_number = models.CharField(primary_key=True, max_length=50, help_text='')  # prefilled by partnumber

    type = models.ForeignKey('Type', null=True, on_delete=models.SET_NULL)
    attributes = JSONField() # Or another dynamic field
    # and more ....


class Type(models.Model):
    """
    Product type i.e. camera-dome, camera-bullet, pir, etc.
    """
    pass


class Segment(models.Model):
    """
    A segment of the company like Industry, Retail, Guarding etc.
    """
    pass


class ProductCategory(models.Model):
    """
    Supposedly a child category of TopCategory.
    """
    pass


class InstallPrice(models.Model):
    """
    Product item installation prices based on Type, Product Category and Segment.
    """
    install_price = models.DecimalField(max_digits=8, decimal_places=2, help_text='')
    type = models.ForeignKey('Type', null=True, on_delete=models.SET_NULL)
    product_category = models.ForeignKey('ProductCategory', null=True, on_delete=models.SET_NULL)
    segment = models.ForeignKey('Segment', null=True, on_delete=models.SET_NULL)

在ProductItem模型中查看“ 属性= HStoreField(db_index = True) ”。

我需要存储在产品项中的主要内容是属性,例如它具有多少个输入/输出/连接选项。我需要将其存储起来,以便在价格模型应用程序中进一步测试彼此之间的产品。这样可以确保您拥有合适数量的具有匹配属性(例如输入或输出)的产品。我还需要User / Admin能够动态添加此属性,以便该应用程序变得自我可持续,并且不需要迁移ID,因为我尚不知道有新属性。

由于我找不到合理的模型配置,因此最终查看了postgres特定字段。这不是必须的!

理想情况下,在管理部分中选择类型时,我希望根据类型显示属性的“预设”。

属性可以是:

inputs  # number
outputs  # number
channels  # number
analysis  # Boolean 

这可以实现吗?由于我的数据库经验有限,因此欢迎提出任何建议。我需要帮助弄清楚模型。

0 个答案:

没有答案
相关问题