2

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!

ramusus
  • 7,789
  • 5
  • 38
  • 45
  • Duplicate: http://stackoverflow.com/questions/353489/cleaner-way-to-query-on-a-dynamic-number-of-columns-in-django – S.Lott Apr 24 '09 at 19:43

1 Answers1

7

Yes.

qs.filter( **{ 'attitudes__%s'%arg:True } )
S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • Thank you! Your case, little modified, works correctly: qs.filter( **{ 'attitudes__%s' % str(attitude): True } ) – ramusus Apr 25 '09 at 08:23
  • 1
    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 Apr 26 '09 at 04:35