我有以下错误: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
返回模型类,而不是对象。正确?
答案 0 :(得分:3)
当你做的时候
.get(attrName1=attr1)
attrName1
实际上是关键字参数,但不是变量。如果要动态命名字段,可以尝试
.get(**{attrName1: attr1})