2

商品属性の値にアクセスしたい。weightcodeと typeで 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

それは機能せず、エラーが発生します:

'ProductAttribute' object has no attribute 'productattributevalue'

oscar のモデルを調べてみましたが、解決策が見つかりませんでした。助けてください。

4

2 に答える 2

5

Django Oscar shipping/scales.py Scale クラスのweight_product() メソッドをコピーすることをお勧めします。

そこでは、製品の「attribute_values」プロパティの「value」プロパティを使用します。

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
于 2014-12-21T17:06:38.943 に答える