On Tue, 2008-12-16 at 21:35 -0800, steverfran...@gmail.com wrote:
> Given I have a variable that's constructed to contain the filter
> clause I want, how do I pass it to the object.filter?
> 
> i.e.
> TripFilter="priceseason__end__gte=datetime.now()"
> 
> yet DiveTrip.objects.filter('TripFilter')
> results in
>  ValueError at /dive/results
> too many values to unpack
> 
> Yet
> DiveTrip.objects.filter(priceseason__end__gte=datetime.now())
> works fine.

That's because f('a=b') and f(a=b) are not the same thing in Python. The
first passes a string to f(), the latter passes the value of "b" to the
argument called "a" in f().

Instead, using the f() case again, for simplicity, you would write

        args = {"a": b}
        f(**args)
        
That is, a dictionary of keyword arguments (often abbreviated as kwargs,
if you're searching). In your filtering example, the kwargs dictionary
would be

        {"priceseason__end__gte": datetime.now()}
        
Notice how this lets you build up the arguments incrementally. Each time
you add a key to the dictionary, it's another argument. Then you pass
them all to the function using the "**" syntax.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to