Django物业简短描述 [英] Django short description for property

查看:171
本文介绍了Django物业简短描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人想知道如何在Django模型中添加自定义名称到属性?例如,如果我有财产:

  @property 
def my_property(self):
return u '返回一些计算'

,我在管理中显示为一列:

  class MyAdmin(admin.ModelAdmin):
list_display = ['my_property',]
/ pre>

然后我看到我的属性列,我需要的是属性X列。我尝试使用my_property.short_description和my_property.verbose_name,这些都不起作用。

解决方案

Masatsugu Hosoi的解决方案工作正常,但您还可以使用装饰器并在属性getter(fget)上设置short_description:

  @property 
def my_property自我):
return u'Returns some calculation'
my_property.fget.short_description = u'Property X'


has anyone an idea how to add custom name to property in Django model? For example, if I got property:

@property
def my_property(self):
     return u'Returns some calculations'

and I show it in admin as a column:

class MyAdmin(admin.ModelAdmin):
    list_display=['my_property',]

Then I see "My property" column and what I need is "Property X" column. I tried with my_property.short_description and my_property.verbose_name, none of this works.

解决方案

The solution by Masatsugu Hosoi works fine, but you can also use the decorator and set short_description on the property getter (fget):

@property
def my_property(self):
    return u'Returns some calculations'
my_property.fget.short_description = u'Property X'

这篇关于Django物业简短描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆