访问django oscar产品属性

too*_*hie 2 python django django-oscar

我想访问产品属性的值.我已经使用代码weight和类型定义了一个product属性float.在我的观点中,我试图访问它,如下所示:

def red(request):
    basket = request.basket
    weight_total = 0
    for line in basket.all_lines():
        weight = line.product.attributes.get(code = 'weight')
        weight_total += weight.productattributevalue
Run Code Online (Sandbox Code Playgroud)

它不起作用并给出错误:

'ProductAttribute' object has no attribute 'productattributevalue'
Run Code Online (Sandbox Code Playgroud)

我试着研究奥斯卡的模型,但找不到解决方案.请帮忙.

Nos*_*.io 5

我建议复制Django Oscar shipping/scales.py Scale class weigh_product()方法.

在那里,他们用"价值"的物业产品的 "ATTRIBUTE_VALUES"属性.

try:
    attr_val = product.attribute_values.get( # Using 'attribute_values'
        attribute__code=self.attribute)      # Use your own code of 'weight'
except ObjectDoesNotExist:
    if self.default_weight is None:          # You can set a default weight.
        raise ValueError("No attribute %s found for product %s"
                         % (self.attribute, product))
    weight = self.default_weight
else:
    weight = attr_val.value                  # << HERE. using 'value' property
Run Code Online (Sandbox Code Playgroud)