tags:

views:

106

answers:

1

Now I have this code:

        attitude = request.REQUEST['attitude']
        if attitude == 'want':
            qs = qs.filter(attitudes__want=True)
        elif attitude == 'like':
            qs = qs.filter(attitudes__like=True)
        elif attitude == 'hate':
            qs = qs.filter(attitudes__hate=True)
        elif attitude == 'seen':
            qs = qs.filter(attitudes__seen=True)

It's will be better to define name of "attitudes__xxxx" dynamically. Is there any ways to do that ?

Thanks!

+7  A: 

Yes.

qs.filter( **{ 'attitudes__%s'%arg:True } )
S.Lott
Thank you! Your case, little modified, works correctly: qs.filter( **{ 'attitudes__%s' % str(attitude): True } )
ramusus
The str(attitude) is redundant -- the %s formatting already does that! So S.Lott's answer is perfect and indeed better than your "little modified" variant.
Alex Martelli