是否可以动态定义函数的参数名称?

时间:2009-04-24 19:36:01

标签: python django

现在我有了这段代码:

        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)

最好动态定义“attitudes__xxxx”的名称。有没有办法做到这一点?

谢谢!

1 个答案:

答案 0 :(得分:7)

qs.filter( **{ 'attitudes__%s'%arg:True } )