Priyanshu Gandhi
Priyanshu Gandhi

Reputation: 35

Django Multiple response of queryset

In the search, I am trying to search the database through title using this code

product_title =  request.GET.get('txtSearch')
status = product.objects.filter(title__icontains=product_title)

And there is another column called product_Subcategory how to get that column using this data?

OR

How to get complete row of this title I have searched?

when I try to print status I got this queryset

<QuerySet [<product: OnePlus 7T Pro (Haze Blue, 8GB RAM, Fluid AMOLED Display, 256GB Storage, 4085mAH Battery)>]>

In which there is no product_Subcategory...if anyone can help...Thanks.

Upvotes: 0

Views: 201

Answers (2)

vikash kumar
vikash kumar

Reputation: 191

In querysets, objects are presented like this. It contains all the attributes values. You can get the subcategory value like this

for product in status:

    subcategory = product.subcategory

or try .values(). It will return list of dictionaries

status = product.objects.filter(title__icontains=product_title).values()

Upvotes: 1

Ashwini Pillai
Ashwini Pillai

Reputation: 16

If you are looking to get all the values for Product, Please use "values", Note that What you did earlier was getting you the Queryset of all 'Product' objects that matched the criteria, but since you just need the values, you can use the following. The result will be a list of dictionaries. Like so,

status = product.objects.filter(title__icontains=product_title).values()

If you just want a specific column value, you can mention that with the 'values' attribute. Like so,

subcategory = 
product.objects.filter(title__icontains=product_title).values('subcategory')

Again this will be a dictionary like [{subcategory:'a'},{subcategory:'b'}]

If you just want the list of subcategories you can try

subcategory_list = list(
product.objects.filter(title__icontains=product_title).values_list('subcategory',flat=True))

The result will be ['a','b']

Upvotes: 0

Related Questions