Wednesday, February 22, 2012

Converting a string representation of query to Django object (Q).

Hi List,
I am just starting to build an application using Django. And my first attempt outside of procedural PHP and Perl-CGI. I am already loving it so much (and ditched other two already).

I am trying to construct the following query at the end.
Result.objects.filter(Q(analysis__experiment__experiment_code="PyLS24") |Q(analysis__experiment__experiment_code="PyLS40"))

How I do now:
The values are passed from a from(post) using checkboxes (#expcodes = request.POST.getlist('expcode')
). So I get a list of:
expcodes = ['PyLS24', 'PyLS40']
then use the for loop to construct string representation of Q object.
expcodes = ['PyLS24', 'PyLS40']
k=''
c=0
for e in expcodes:
j=e.join(['Q(analysis__experiment__experiment_code="','") '])
if c == 0:
k = j
else:
k='|'.join([k, j])
c = c+1

k in this gives me 'Q(analysis__experiment__experiment_code="PyLS24") |Q(analysis__experiment__experiment_code="PyLS40")  '.
When i use this to query the model as following... Result.objects.filter(k) I get "ValueError: too many values to unpack" error. But, exact string, if I copy paste into Results.object.filter(here) it works. 

When I checked the type, k constructed from for loop is in string format (obviously). But, when I copy paste the pint out of k, it automatically becomes Django's Q object.

Can anyone help me to sort this out. Or is there altogether a better way to do this query. I would really appreciate any help.


Gowthaman






--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/_C25aFV3OUcJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment