Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下代码,我需要使用 setattr 从字符串生成变量并将其设置为等于amn.
amn
for i in amenities: if i in request.GET: amn = request.GET.getlist(i) propertiesList = Property.objects.filter(setattr(i+"__title__in", amn))
似乎 setattr 需要 3 个参数,我怎么能只用这 2 个参数呢?
不幸的是,你不能用它setattr来实现你想要的,试试这个:
setattr
propertiesList = Property.objects.filter(**{i+"__title__in": amn})