Re: Bug when creating Q object from string representation of another Q object?
Yes - sorry about the typo. As you say, the "orig" should be filter_orig - that was a cut and paste mistake. I was playing around and I think I just mistakenly assumed that I could create a Q from the string representation of another Q. Sounds like that was a mistaken assumption. What I have is a string representation that is a prefix notation that is very similar to what you get when you print a Q. IE, it is easy for me to generate something like this: (OR (AND: ('owner__username', 'mlevine'), ('status', 'open')), ('priority', 'critical')) Where this means (Q(owner_username='mlevine') & Q(status='open')) | Q (priority='critical') Actually, what I have is just plain xml. Through basic regexp substitution I am able to turn it into the above prefix notation with minimal effort. So I was trying to figure out if there was a way I could easily create a Q without loading my prefix notation into a tree and recursing into the tree to create the Q from the leaves up. I was pleasantly surprised when I was able to construct a Q off of the string rep of another Q, but it looks like that it is not really doing what I thought it was doing. Ok, back to the drawing board. Margie On Jul 22, 5:02 am, Russell Keith-Mageewrote: > On Wed, Jul 22, 2009 at 5:52 AM, Margie wrote: > > > I have a situation where I want to do the following: > > take a bunch of POST params and from them create a Q object > > urlencode that Q object and turn it into a GET param, redirect > > using that param > > process the GET that contains the urlencoded Q object, create a > > Q object from it, and use it in a filter > > > I think this should all be possible, however, I am having trouble > > recreating the new Q object from the string representation of the > > original Q object. Here's an example of the problem: > > filter_orig = Q(owner__username='mlevine') > print filter_orig > > (AND: ('owner__username', 'mlevine')) > filter_new = Q("%s" % orig) > > This is the line that looks a bit suspect to me. Firstly - I'm > assuming `orig` should actually be `filter_orig` - in which case... > > print filter_new > > (AND: (AND: ('owner__username', 'mlevine'))) > > This isn't quite what you think it is. You are reading it as an AND > whose first term is an AND. I suspect what you are actually getting is > an AND clause whose first term is a string that starts "(AND:" > > When you construct filter_new, you don't pass in a string - you're > passing in a dictionary of kwargs. The owner__username='mlevine' > syntax is a keyword argument, not a string. > > Your original problem descriptions seems to suggest that you think you > will be able to pass a string into Q() and have it interpreted as a > query. This isn't the case - the arguments to Q() are no different to > the arguments to filter(). If you want to serialize a query for use in > a GET request, you'll need to find a different way to serialize your > query. > > Yours, > Russ Magee %-) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Bug when creating Q object from string representation of another Q object?
On Wed, Jul 22, 2009 at 5:52 AM, Margiewrote: > > I have a situation where I want to do the following: > take a bunch of POST params and from them create a Q object > urlencode that Q object and turn it into a GET param, redirect > using that param > process the GET that contains the urlencoded Q object, create a > Q object from it, and use it in a filter > > > I think this should all be possible, however, I am having trouble > recreating the new Q object from the string representation of the > original Q object. Here's an example of the problem: > filter_orig = Q(owner__username='mlevine') print filter_orig > (AND: ('owner__username', 'mlevine')) filter_new = Q("%s" % orig) This is the line that looks a bit suspect to me. Firstly - I'm assuming `orig` should actually be `filter_orig` - in which case... print filter_new > (AND: (AND: ('owner__username', 'mlevine'))) This isn't quite what you think it is. You are reading it as an AND whose first term is an AND. I suspect what you are actually getting is an AND clause whose first term is a string that starts "(AND:" When you construct filter_new, you don't pass in a string - you're passing in a dictionary of kwargs. The owner__username='mlevine' syntax is a keyword argument, not a string. Your original problem descriptions seems to suggest that you think you will be able to pass a string into Q() and have it interpreted as a query. This isn't the case - the arguments to Q() are no different to the arguments to filter(). If you want to serialize a query for use in a GET request, you'll need to find a different way to serialize your query. Yours, Russ Magee %-) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Bug when creating Q object from string representation of another Q object?
You don't give us enough explanation about your error. What type of error have you got ? What is your orig variable ? How do you serialize your Q object ? Are you sure that a Q object can be instantiated like that ? Frédéric 2009/7/21 Margie: > > I have a situation where I want to do the following: > take a bunch of POST params and from them create a Q object > urlencode that Q object and turn it into a GET param, redirect > using that param > process the GET that contains the urlencoded Q object, create a > Q object from it, and use it in a filter > > > I think this should all be possible, however, I am having trouble > recreating the new Q object from the string representation of the > original Q object. Here's an example of the problem: > filter_orig = Q(owner__username='mlevine') print filter_orig > (AND: ('owner__username', 'mlevine')) filter_new = Q("%s" % orig) print filter_new > (AND: (AND: ('owner__username', 'mlevine'))) Task.objects.filter(filter_orig) > [, ] Task.objects.filter(filter_new) > Traceback (most recent call last): > ... > File "/home/mlevine/django/django_libs/django-admin-ui-july8/lib/ > python2.6/site-packages/django/db/models/sql/query.py", line 1520, in > add_filter > arg, value = filter_expr > > > Note that in the above log, when I print filter_new, there is an extra > AND at the front. Logically this shouldn't be be a problem, but I > suspect that it is indicative of the problem. > > Is what I'm doing above supposed to work? Is this a bug? > > This is using 1.1 beta. > > Margie > > > > > > -- http://www.openidfrance.fr/fhebert --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Bug when creating Q object from string representation of another Q object?
I have a situation where I want to do the following: take a bunch of POST params and from them create a Q object urlencode that Q object and turn it into a GET param, redirect using that param process the GET that contains the urlencoded Q object, create a Q object from it, and use it in a filter I think this should all be possible, however, I am having trouble recreating the new Q object from the string representation of the original Q object. Here's an example of the problem: >>> filter_orig = Q(owner__username='mlevine') >>> print filter_orig (AND: ('owner__username', 'mlevine')) >>> filter_new = Q("%s" % orig) >>> print filter_new (AND: (AND: ('owner__username', 'mlevine'))) >>> Task.objects.filter(filter_orig) [, ] >>> Task.objects.filter(filter_new) Traceback (most recent call last): ... File "/home/mlevine/django/django_libs/django-admin-ui-july8/lib/ python2.6/site-packages/django/db/models/sql/query.py", line 1520, in add_filter arg, value = filter_expr Note that in the above log, when I print filter_new, there is an extra AND at the front. Logically this shouldn't be be a problem, but I suspect that it is indicative of the problem. Is what I'm doing above supposed to work? Is this a bug? This is using 1.1 beta. Margie --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---