【问题标题】:Accessing model field attributes in Django在 Django 中访问模型字段属性
【发布时间】:2011-06-06 18:30:08
【问题描述】:

我在 Django 1.2.4 中有一个模型:

class MyModel():
    foo = IntegerField(verbose_name="bar")

    def printFoo(self):
        print("Value of %s is %d" % (foo.verbose_name, foo))

我正在尝试获取字段的值和详细名称。我该怎么做?

我查看了myModel._meta.fields,但我不确定这是否可行。

【问题讨论】:

    标签: python django models


    【解决方案1】:

    大概是这样的:

    MyModel._meta.get_field('foo').verbose_name
    

    请参阅How can I programmatically obtain the max_length of a Django model field? 了解非常相似的问题。

    【讨论】:

      猜你喜欢
      • 2014-11-23
      • 2014-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 2017-05-04
      • 1970-01-01
      相关资源
      最近更新 更多