简体   繁体   English

访问Django中的模型字段属性

[英]Accessing model field attributes in Django

I have a model in Django 1.2.4: 我在Django 1.2.4中有一个模型:

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

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

I'm trying to get both the value and verbose name of a field. 我正在尝试获得字段的值和详细名称。 How can I do this? 我怎样才能做到这一点?

I've looked at myModel._meta.fields , but I'm not sure if that's the way to go. 我看过myModel._meta.fields ,但我不确定是不是这样。

Probably like this: 可能是这样的:

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

See How can I programmatically obtain the max_length of a Django model field? 请参阅如何以编程方式获取Django模型字段的max_length? for a very similar question. 对于一个非常相似的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM