Thursday, April 22, 2021

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

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

On Thursday, 22 April 2021 at 13:53:18 UTC+2 bhathiy...@gmail.com wrote:


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


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ad89068d-d98c-4f08-93da-d172c789e889n%40googlegroups.com.

No comments:

Post a Comment