如何从 Django 中的以下数据中仅获取 'title' 属性值
how to get only the 'title' attribute value from following data in django
这是 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 = choosee[0].get('title')
print (title)
这是 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 = choosee[0].get('title')
print (title)