Friday, February 28, 2020

Re: Django values_list queryset returns queryset object instead of list

HI Santhosh,

This is expected behaviour because it is a queryset until you query the results - or iterate over them.

For example:
for item in View.objects.all().values_list('name',flat=True).order_by('id'):
  print(item)

will iterate over the list of values. BUT if you want to evaluate the list directly you can do this:
views = list(View.objects.all().values_list('name',flat=True).order_by('id'))

And you will have a regular list.

Regards,

Andréas


Den ons 26 feb. 2020 kl 17:39 skrev Santhosh sridhar <santhosh.sri27@gmail.com>:
Hi,
I have a table named View, I'm filtering values like this : views = View.objects.all().values_list('name',flat=True).order_by('id')
I get the output as <QuerySet ['lib', 'lef', 'models', 'datasheet', 'layout', 'gds', 'netlist', 'gds2def', 'symbol']> instead of list of the names. So I face error when try to parse it in jquery : jQuery.parseJSON(JSON.stringify("{{views|safe}}".replace(/&#39;/g, '"').replace(/u&quot;/g, '"').replace(/&quot;/g, '"'))); it gives me the result as  ------ > <QuerySet ['lib', 'lef', 'models', 'datasheet', 'layout', 'gds', 'netlist', 'gds2def', 'symbol']> .

How to fix this? Appreciating any help.


Regards,
Santhosh

--
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/93ee9e2e-4f23-4de5-bd31-3dba75d5d26b%40googlegroups.com.

--
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/CAK4qSCddJirMUNCQjP7Eg%2BdXBmhnLTRYtudCk3FbpAUip-sEcQ%40mail.gmail.com.

No comments:

Post a Comment