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.
我无法使函数 getattr 工作。这是我的代码:
print ConfigConsModel()._meta.get_all_field_names() #['codesectrepmodel', 'configCons', 'id'] modelInstance=ConfigConsModel() newAttrName1=getattr(modelInstance, "configCons") print newAttrName1 #empty -> PB
怎么了?
modelInstance=ConfigConsModel()
这初始化modelInstance为ConfigConsModel类的新(空)实例
modelInstance
ConfigConsModel
newAttrName1=getattr(modelInstance, "configCons")
这条线相当于
newAttrName1=modelInstance.configCons
它没有获取属性的名称,而是获取它的值。这当然是空的。