在Django-Oscar中以编程方式定义ProductAttributes的位置

时间:2017-08-09 16:48:58

标签: python django python-3.x django-oscar

在为奥斯卡商业项目开发运输方法时,我发现我需要两个属性;一个包含表示产品重量的浮点值(我使用Scale class来衡量产品)和一个链接到集装箱模型的实体属性。

需要将属性分配给任何需要运送的产品类。我在哪里/如何创建它们?我有以下代码,但我不确定它适合的位置。

from oscar.core.loading import get_model
ProductAttribute = get_model('catalogue', 'ProductAttribute')

ProductAttribute.objects.get_or_create(code='weight',
        product_class=[product class],
        defaults={
            'name': 'Weight',
            'type': ProductAttribute.FLOAT,
            })
ProductAttribute.objects.get_or_create(code='box',
        product_class=[product class],
        defaults={
            'name': 'Box used for shipping'
            'type': ProductAttribute.ENTITY,
        })

添加此代码的最佳位置在哪里?

1 个答案:

答案 0 :(得分:2)

标准的Django方法是使用data migration

如果您不希望自动加载数据,则替代方法是使用fixture

我们通常只是通过管理员设置属性。此外,PR #2448旨在为添加属性添加仪表板支持。