Search code examples
pythondjangodjango-rest-frameworkdjango-oscar

*** TypeError: get_value_by_attribute() missing 1 required positional argument: 'attribute'


I am using Django oscar and trying to get product attributes key and values using product.attr.get_value_by_attribute(). And I am getting above errors and I have tried to pass attribute and it still giving me an error.

My testing debugger code is as follows:

(Pdb) child_product
<Product: Good Day 100 gm>
(Pdb) child_product.__dict__
{'_state': <django.db.models.base.ModelState object at 0x7fa088e1c438>, 'id': 13, 'structure': 'child', 'upc': '', 'parent_id': 12, 'title': 'Good Day 100 gm', 'slug': 'good-day-100-gm', 'description': '', 'product_class_id': None, 'rating': None, 'date_created': datetime.datetime(2019, 6, 22, 15, 30, 24, 273252, tzinfo=<UTC>), 'date_updated': datetime.datetime(2019, 6, 22, 15, 30, 24, 273270, tzinfo=<UTC>), 'is_discountable': True, 'attr': <oscar.apps.catalogue.product_attributes.ProductAttributesContainer object at 0x7fa088e1c470>}
(Pdb) child_product.attr
<oscar.apps.catalogue.product_attributes.ProductAttributesContainer object at 0x7fa088e1c470>
(Pdb) child_product.attr.__dict__
{'product': <Product: Good Day 100 gm>, 'initialised': False}
(Pdb) child_product.attr.__dict__
{'product': <Product: Good Day 100 gm>, 'initialised': False}
(Pdb) child_product.attr.get_values()
<QuerySet [<ProductAttributeValue: Brand: Britania>, <ProductAttributeValue: Variant Name: 100 gm x 12>]>
(Pdb) child_product.attr.get_all_attributes()
<QuerySet [<ProductAttribute: Brand>, <ProductAttribute: GST Rate>, <ProductAttribute: HSN Code>, <ProductAttribute: Liquid Content>, <ProductAttribute: Parent Company>, <ProductAttribute: Promo SKU>, <ProductAttribute: Selling Lot Size>, <ProductAttribute: Sub Brand>, <ProductAttribute: Tags>, <ProductAttribute: Variant Name>, <ProductAttribute: Weight>, <ProductAttribute: Weight Unit>]>
(Pdb) child_product.attr.get_value_by_attribute()
*** TypeError: get_value_by_attribute() missing 1 required positional argument: 'attribute'
(Pdb) child_product.attr.get_value_by_attribute(attribute="Brand")
*** ValueError: invalid literal for int() with base 10: 'Brand'

So here I am able to get value and all attributes but I don't know how to get value by attributes which I am needed.

child_product.attr.get_value_by_attribute()

So anyone has any idea about this then please help. Definitively your help will be appreciated.


Solution

  • When getting value by attributes, an instance of attribute has to be passed in the get_value_by_attribute method. e.g.

    brand = child_product.attr.get_all_attributes().first()
    child_product.attr.get_value_by_attribute(attribute=brand)
    

    You can also retreive values by attribute by using its name invoking get on the result of get_values method. e.g.

    child_product.attr.get_values().get(attribute__name='Brand')