List Info

Thread: dynamic filtering in a QuerySet




dynamic filtering in a QuerySet
user name
2007-08-30 11:46:48
How can I apply dynamic filtering to a Django QuerySet?

widgets = Widget.objects.all()[:10]
filters = {'foo':1,'bar':1,'baz':1}
for f in filters:
    widgets = widgets.filter(f=filters[f])

This breaks down because I can't figure out how to use an
expression
as a named parameter.

I've tried eval() and all sort of back tick and quoting
combinations,
nothing seems to work.

Thanks,


-- 
Greg Donald
http://destiney.com/

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


Re: dynamic filtering in a QuerySet
country flaguser name
United States
2007-08-30 11:48:31
widgets = widgets.filter( **filters )


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


Re: dynamic filtering in a QuerySet
user name
2007-08-30 12:04:00
On 8/30/07, daev <daevaorngmail.com> wrote:
>
> widgets = widgets.filter( **filters )

When I try that I get the error:

filter() keywords must be strings

Does that have anything to do with the fact that they are
unicode?

filters: {u'LOB': u'B'}



-- 
Greg Donald
http://destiney.com/

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


Re: dynamic filtering in a QuerySet
country flaguser name
United States
2007-08-30 12:10:03
Use strings instead of unicode. Key word arguments names
must be
strings.


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


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )