2

我有以下错误:Cannot resolve keyword 'attrName1' into field. Choices are: codesectrepmodel, configCons, id。我的代码:

modelName1="ConfigConsModel"
model1 = get_model('actsInformationRetrieval', modelName1)
print "model attr", model1._meta.get_all_field_names() #displays ['codesectrepmodel', 'configCons', 'id']
print "attrName1", attrName1 #displays configCons
print "attr1", attr1 #displays ECOFIN
attr1Instance=model1.objects.get(attrName1=attr1)

怎么了?我认为问题在于get_model返回模型类,而不是对象。正确的?

4

1 回答 1

5

当你在做

.get(attrName1=attr1)

attrName1实际上是关键字参数,但不是变量。如果您想动态命名字段,您可以尝试

.get(**{attrName1: attr1})
于 2013-07-25T13:23:20.897 回答