how to get only the 'title' attribute value from following data in django

20 views
Skip to first unread message

Bhathiya Amarasinghe

unread,
Apr 22, 2021, 7:53:18 AM4/22/21
to Django users


This is the views.py I want to get 'title' attribute from the serialize data

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)

Output : 

[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')]))])]


Derek

unread,
Apr 23, 2021, 2:09:59 AM4/23/21
to Django users
I'd use:

querye = Product.objects.filter(id=sell_id).values('title')
title = querye[0]['title']  # get the title for the first Product

But if you're sure there is one (and only one) product matching the 'sell_id', you can use:

querye = Product.objects.get(id=sell_id)
title = querye.title

HTH
Reply all
Reply to author
Forward
0 new messages