[Django]-Django short description for property

60👍

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'
👤bbrik

14👍

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")
👤vdboor

5👍

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

3👍

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')

1👍

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'

0👍

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.

-1👍

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.

Leave a comment