alekwisnia
alekwisnia

Reputation: 2354

Django short description for property

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.

Upvotes: 34

Views: 20685

Answers (7)

hassanzadeh.sd
hassanzadeh.sd

Reputation: 3461

in admin.py into your ModelAdmin add this code :

def my_property(self , obj):
    return 'Returns some calculations'
my_property.short_description = 'Property X'

after that add my_property to list_display

list_display=['my_property',]

like this :

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

    def my_property(self , obj):
        return 'Returns some calculations'
    my_property.short_description = 'Property X'

Upvotes: 1

bbrik
bbrik

Reputation: 3036

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'

Upvotes: 62

Indradhanush Gupta
Indradhanush Gupta

Reputation: 4237

This thread is old, but in case someone wanders down here, the solution that works is to just add a short_description on the method/property in your class.

def my_property(self):
    # Some things done
my_property.short_description = 'Property'

This will solve the problem.

Upvotes: -1

ellethee
ellethee

Reputation: 181

You can redefine the property class as New-style class. So you can add all attributes you need

class property(property):
    """Redefine property object"""
    pass

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

my_property.short_description = _("Property X")

Actually I'm not sure it is a good practice. Anyway I don't see any contraindication.

Upvotes: 0

Masatsugu Hosoi
Masatsugu Hosoi

Reputation: 420

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

Upvotes: 5

vdboor
vdboor

Reputation: 22526

I've ended up adding the field to the ModelAdmin class as well, redirecting to the model:

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

    def my_property(self, object):
        return object.my_property

    my_property.short_description = _("Property X")

Upvotes: 15

Brandon Taylor
Brandon Taylor

Reputation: 34553

I don't know how to do this with the @ decorator syntax, but you can do:

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

Then, in your admin class set:

list_display = ['whatever', 'something_else', 'property_x']

The capitalization isn't perfect, but it will display 'Property x'. It may be possible to get Django to display the __doc__ for a property so you could control the formatting better, but it's a start.

You can add the doc= keyword argument as such:

property_x = property(my_property, doc='Property X')

Upvotes: 3

Related Questions