【问题标题】:how to get only the 'title' attribute value from following data in django如何从 django 中的以下数据中仅获取“标题”属性值
【发布时间】:2021-04-22 08:57:10
【问题描述】:

这是views.py,我想从序列化数据中获取'title'属性

views.py

class CalculatView(views.APIView):
    query = CartProduct.objects.latest('id')
    serializer = CartProductSerializer(query)
    choose_product = serializer.data.get('product')
    [sell_id] = choose_product
    querye = Product.objects.filter(id = sell_id)
    serializere = ProductSerializers(querye, many=True)
    choosee = serializere.data
   
    print(choosee)

输出:

[OrderedDict([('id', 2), ('title', 'Frock'), ('date', '2021-04-22'), ('image', '/media/products/kids2.jpg'), ('marcket_price', 1.2), ('selling_price', 1.2), ('description', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaa'), ('category', OrderedDict([('id', 2), ('title', 'Fashion'), ('date', '2021-04-22')])), ('seller', OrderedDict([('id', 2), ('seller_name', 'CIB - Piliyandala'), ('lat', 6.8018), ('lng', 79.9227), ('date', '2021-04-22')]))])]

【问题讨论】:

  • 将“title”属性添加到您的ProductSerializers 类字段

标签: django django-models django-rest-framework django-views python-3.6


【解决方案1】:

试试这个。

title = choosee[0].get('title')
print (title)

【讨论】:

  • 谢谢你的回复。它说 AttributeError: 'ReturnList' object has no attribute 'get'
  • 哈。我懂了。是清单。我的错。我已经更新了答案检查是否适合您
猜你喜欢
  • 2017-02-27
  • 1970-01-01
  • 1970-01-01
  • 2013-11-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-15
  • 1970-01-01
  • 2013-09-23
相关资源
最近更新 更多