Django:动态访问模型的属性
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django:动态访问模型的属性相关的知识,希望对你有一定的参考价值。
example: a method on a model that outputs a list of tuples with (attribute_name.verbose, attribute_value)
def attrs_verbose(self): model = self.__class__ # using this form: Record._meta.get_field('created_by').verbose_name items = [] for k, v in self.__dict__.items(): try: x = model._meta.get_field(k).verbose_name except: x = k items += [(x, v)] items.sort() return items
以上是关于Django:动态访问模型的属性的主要内容,如果未能解决你的问题,请参考以下文章