[Fixed]-Verbose name for @property


A verbose_name cannot be set for a @property called name, but short_description can be used instead:

class Person(models.Model):
    first_name = models.CharField('Given name', max_length=255)
    last_name = models.CharField('Family name ', max_length=255)

    def name(self):
        return '%s %s' % (self.first_name, self.last_name)

    name.fget.short_description = 'First and last name'

This may not work with all Django admin classes, but it will work with the basic one.

This uses the fget object created by @property and sets a short_description property in it. Django looks for that property and uses it if available.


In the documentation the following code is suggested to give a property short description:

class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)

    def my_property(self):
        return self.first_name + ' ' + self.last_name
    my_property.short_description = "Full name of the person"

    full_name = property(my_property)

class PersonAdmin(admin.ModelAdmin):
    list_display = ('full_name',)

However, this approach does not work when using Django-tables2 so the following code is needed in order to change a column name:

columnName = tables.Column(verbose_name='column name')

Django-tables2: Change text displayed in column title

so finally I think you should use custom forms and override field verbose_name if you face such a problem.


Leave a comment