【问题标题】:*** TypeError: get_value_by_attribute() missing 1 required positional argument: 'attribute'*** 类型错误:get_value_by_attribute() 缺少 1 个必需的位置参数:“属性”
【发布时间】:2019-06-28 18:25:19
【问题描述】:

我正在使用 Django oscar 并尝试使用 product.attr.get_value_by_attribute() 获取产品属性键和值。而且我遇到了错误,我试图传递属性,但它仍然给我一个错误。

我的测试调试器代码如下:

(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'

所以在这里我能够获得价值和所有属性,但我不知道如何通过我需要的属性获得价值。

child_product.attr.get_value_by_attribute()

所以任何人对此有任何想法,请帮助。毫无疑问,您的帮助将不胜感激。

【问题讨论】:

  • 上面写着ValueError: invalid literal for int() with base 10: 'Brand'attribute 应该是一个数字。

标签: python django django-rest-framework django-oscar


【解决方案1】:

通过属性获取值时,必须在get_value_by_attribute 方法中传递属性实例。 例如

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

您还可以通过在 get_values 方法的结果上使用 its name 调用 get 来按属性检索值。 例如

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

【讨论】:

  • child_product.attr.get_value_by_attribute(attribute__name='Brand') 它给了*** TypeError: get_value_by_attribute() got an unexpected keyword argument 'attribute__name'你对此有什么想法
  • 原因是 get_value_by_attribute 传递了它自己的论点。我已经更新了获取品牌属性值的方法
  • 是的,现在它工作正常,但要获取属性名称和值,attribute_summary 是更好的选择。但到目前为止,我很高兴我的错误得到了解决。谢谢。
【解决方案2】:

在我看来,属性对象需要作为 get_value_by_attribute() 的参数传入。如果“品牌”是您试图用来检索特定属性的 slug,请使用 get_attribute_by_slug() 获取属性对象,然后将其传递给 get_value_by_attribute();

【讨论】:

    【解决方案3】:

    最后,我找到了一个非常简单的解决方案,如下所示

    (Pdb) child_product.attribute_summary
    'Brand: Nestle, Variant Name: Rs 5 x 126'
    
    (Pdb) child_product.attribute_summary.split(',')
    ['Brand: Nestle', ' Variant Name: Rs 5 x 126']
    
    

    在这里,我得到了我想要的属性名称和值,如果您使用 Django oscar,我希望这对您也有帮助。

    【讨论】:

      猜你喜欢
      • 2020-06-11
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      • 2021-08-05
      相关资源
      最近更新 更多