首页 > 解决方案 > 如何从 django 中的以下数据中仅获取“标题”属性值

问题描述

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

视图.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')]))])]

标签: djangodjango-modelsdjango-rest-frameworkdjango-viewspython-3.6

解决方案


尝试这个。

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

推荐阅读