PerroVerd
PerroVerd

Reputation: 955

Modify on-the-fly verbose_name in a model field on django admin

I have this sample model working with the admin

class Author(models.Model):
    name = models.CharField(_('Text in here'), max_length=100)

with verbose_name set as ugettext_lazy 'Text in here', but sometimes, depending on the site_id i want to present a diferent verbose name, so I modified the init in this way

def __init__(self, *args, **kwargs):
    super(Author, self).__init__(*args, **kwargs)
    #some logic in here
    self._meta.get_field('name').verbose_name = _('Other text')

It works, displaying the 'Other text' instead the 'Text in here'... except for the very first time the author/add view is used.

¿Is it the right way to do it? ¿How can i fix the first time problem?

Thanks in advance

Upvotes: 4

Views: 4269

Answers (2)

DSAnup
DSAnup

Reputation: 23

You can simply add verbose_name

class Author(models.Model):
    name = models.CharField(max_length=100, verbose_name = 'Other text')

Upvotes: -1

Daniel Roseman
Daniel Roseman

Reputation: 599450

Don't modify elements of the model. There's all sorts of metaclass stuff going on in model definitions that will break things, as you have discovered.

Instead, define a custom form and change the field label in the __init__ method there.

Upvotes: 8

Related Questions