2014-12-21 38 views
2

我想訪問產品屬性的值。我已經使用代碼weight和類型float定義了產品屬性。在我的意見,我想如下訪問:訪問django oscar產品屬性

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' 

我試圖尋找到奧斯卡的模式,但無法找到解決方案。請幫忙。

回答

4

我會建議複製Django的奧斯卡運輸/ scales.py規模類weigh_product() method.

在那裏,他們用「價值」的產品「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 
1

獲取或通過代碼設置屬性,只是做:

product.attr.code 

例如,如果你的屬性的代碼是 「重」:

product.attr.weight = 125 

我發現,例如在oscar.apps.catalogue.product_attributes(django-oscar 1.4)的代碼中,它可以在python和模板中使用。