【问题标题】:Where to programmatically define ProductAttributes in Django-Oscar在 Django-Oscar 中以编程方式定义 ProductAttributes 的位置
【发布时间】:2017-08-09 16:48:58
【问题描述】:

在为 Oscar Commerce 项目开发运输方法时,我发现我需要两个属性;一个包含表示产品重量的浮点值(我使用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,
        })

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

【问题讨论】:

  • 我将它添加到夹具上用于测试目的,并在生产环境中迁移。

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


【解决方案1】:

标准的 Django 方法是使用 data migration

如果您不希望自动加载数据,那么另一种方法是使用fixture

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

【讨论】:

    猜你喜欢
    • 2019-11-01
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 2018-02-28
    相关资源
    最近更新 更多