Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verbose name for @property

I have a @property called name in my

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

    @property
    def name(self):
        return "%s %s" % (self.first_name, self.last_name)

Is it easily possible to specify the verbose_name for this property?

like image 458
SaeX Avatar asked Mar 21 '14 16:03

SaeX


1 Answers

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)

    @property
    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.

like image 100
Mr. Lance E Sloan Avatar answered Sep 29 '22 16:09

Mr. Lance E Sloan