當前位置:成語大全網 - 新華字典 - django遍歷model裏面的屬性字段

django遍歷model裏面的屬性字段

具體的寫法是

results?=?ServerInformation.objects.get(id?=?1)#filter是queryset,沒有_meta方法

allhost?=?ServerInformation._meta.get_all_field_names()#這句沒錯

vername?=?ServerInformation._meta.get_field('ServerType').verbose_name#這句也沒錯,S?erverType是該模型的壹個屬性。

vervalue?=?ServerInformation._meta.get_field('ServerZone').default?#即可獲取到默認的值,話說妳都懂得獲取到verbose_name,怎麽不會想到直接.default呢。

ps:

getattr(object,?name[,?default])

Return the value of the named attribute of?object.?name?must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example,

getattr(x,?'foobar') is equivalent to

?x.foobar

. If the named attribute does not exist,?default?is returned if provided, otherwise?AttributeError?is raised.

看看怎麽使用。