Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django: getattr function (get field name)

Tags:

django

getattr

I can't make the function getattr work. Here is my code:

print ConfigConsModel()._meta.get_all_field_names() #['codesectrepmodel', 'configCons', 'id']
modelInstance=ConfigConsModel()
newAttrName1=getattr(modelInstance, "configCons")
print newAttrName1 #empty -> PB

What's wrong?

like image 372
rom Avatar asked Jan 18 '26 16:01

rom


1 Answers

modelInstance=ConfigConsModel()

This initializes modelInstance as new (empty) instance of ConfigConsModel class

newAttrName1=getattr(modelInstance, "configCons")

This line is equivalent to

newAttrName1=modelInstance.configCons

It does not get attribute's name, it gets it's value. Which of course is empty.

like image 155
vartec Avatar answered Jan 20 '26 07:01

vartec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!