33

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.

7 Answers 7

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'
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")
5
def _my_property(self):
    return u'Returns some calculations'  
_my_property.short_description =  'Property X'
my_property = property(_my_property)
2
  • Best solution. Easy and direct Jun 10, 2015 at 8:35
  • 2
    except that you end up with a prop and a method that return the same Nov 13, 2017 at 16:51
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')
4
  • 1
    doc=keyword would be much better. What I need is a name of column that would be localizable. For example _('Property X'). That's why I don't want Django just to capitalize property's name. How could I use doc with decorators?
    – alekwisnia
    Aug 31, 2011 at 9:50
  • Good question. I haven't had time to dig through Django's implementation of how the column names in the change list get populated. Aug 31, 2011 at 13:52
  • It would be nice if they changed the behavior of list_display, so that one can customize column names, ie.: list_display = ['whatever':'First column name', 'something_else':'Second column','property_x':'Third column',]
    – alekwisnia
    Sep 1, 2011 at 7:55
  • I agree, and since all the source is available to us, we can write a patch. Sep 1, 2011 at 13:35
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.

1
  • Works fine as of Django 2.1.
    – Endre Both
    Jan 15, 2019 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.