Quantcast

Building filter strings dynamically

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Building filter strings dynamically

Greg Fuller

Hello,

I'm trying to build filter strings dynamically.

The normal way works:
    qry = qry.filter(sections__name__exact='printing')

This does not work:
    filter_str = "sections__name__exact='shooting'"
    qry = qry.filter(filter_str)
.
The error is "too many values to unpack" at django/db/models/sql/
query.py in add_filter, line 933

I realize something outside normal name-spacing is probably happening,
since "sections__name__exact"  doesn't have to be defined anywhere.

But is there any way to build the parameter to the filter dynamically?

Thanks in advance,

Greg F.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Building filter strings dynamically

John Lenton

On Fri, May 9, 2008 at 6:19 PM, Greg Fuller <[hidden email]> wrote:

>
> Hello,
>
> I'm trying to build filter strings dynamically.
>
> The normal way works:
>    qry = qry.filter(sections__name__exact='printing')
>
> This does not work:
>    filter_str = "sections__name__exact='shooting'"
>    qry = qry.filter(filter_str)
> .
> The error is "too many values to unpack" at django/db/models/sql/
> query.py in add_filter, line 933
>
> I realize something outside normal name-spacing is probably happening,
> since "sections__name__exact"  doesn't have to be defined anywhere.
>
> But is there any way to build the parameter to the filter dynamically?

the usual python way of building dynamic args:

filter = {'sections__name__exact': 'shooting'}
qry = qry.filter(**filter)

--
John Lenton ([hidden email]) -- Random fortune:
The trouble with a lot of self-made men is that they worship their creator.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Building filter strings dynamically

Aaron Fay
Similarly, this will work also:
field = 'sections'
qry = qry.filter(**{field+'__name__exact': 'shooting'})

Cheers,
Aaron


John Lenton wrote:
On Fri, May 9, 2008 at 6:19 PM, Greg Fuller [hidden email] wrote:
  
Hello,

I'm trying to build filter strings dynamically.

The normal way works:
   qry = qry.filter(sections__name__exact='printing')

This does not work:
   filter_str = "sections__name__exact='shooting'"
   qry = qry.filter(filter_str)
.
The error is "too many values to unpack" at django/db/models/sql/
query.py in add_filter, line 933

I realize something outside normal name-spacing is probably happening,
since "sections__name__exact"  doesn't have to be defined anywhere.

But is there any way to build the parameter to the filter dynamically?
    

the usual python way of building dynamic args:

filter = {'sections__name__exact': 'shooting'}
qry = qry.filter(**filter)

  

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Building filter strings dynamically

Greg Fuller

Thanks to both.  Got it working.

On May 9, 9:17 pm, Aaron Fay <[hidden email]> wrote:
> Similarly, this will work also:field = 'sections' qry = qry.filter(**{field+'__name__exact': 'shooting'})Cheers,
> Aaron
> John Lenton wrote:On Fri, May 9, 2008 at 6:19 PM, Greg Fuller<[hidden email]>wrote:Hello, I'm trying to build filter strings dynamically. The normal way works: qry = qry.filter(sections__name__exact='printing') This does not work: filter_str = "sections__name__exact='shooting'" qry = qry.filter(filter_str) . The error is "too many values to unpack" at django/db/models/sql/ query.py in add_filter, line 933 I realize something outside normal name-spacing is probably happening, since "sections__name__exact" doesn't have to be defined anywhere. But is there any way to build the parameter to the filter dynamically?the usual python way of building dynamic args: filter = {'sections__name__exact': 'shooting'} qry = qry.filter(**filter)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Building filter strings dynamically

Andy Baker-2

I struggled to work this out and it's one of those problems that's
hard to work out what you should Google for.

It would make a worthy addition to the docs IMHO.

On May 10, 4:08 am, Greg Fuller <[hidden email]> wrote:
> Thanks to both.  Got it working.
>
> On May 9, 9:17 pm, Aaron Fay <[hidden email]> wrote:
>
> > Similarly, this will work also:field = 'sections' qry = qry.filter(**{field+'__name__exact': 'shooting'})Cheers,
> > Aaron
> > John Lenton wrote:On Fri, May 9, 2008 at 6:19 PM, Greg Fuller<[hidden email]>wrote:Hello, I'm trying to build filter strings dynamically. The normal way works: qry = qry.filter(sections__name__exact='printing') This does not work: filter_str = "sections__name__exact='shooting'" qry = qry.filter(filter_str) . The error is "too many values to unpack" at django/db/models/sql/ query.py in add_filter, line 933 I realize something outside normal name-spacing is probably happening, since "sections__name__exact" doesn't have to be defined anywhere. But is there any way to build the parameter to the filter dynamically?the usual python way of building dynamic args: filter = {'sections__name__exact': 'shooting'} qry = qry.filter(**filter)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Loading...