views:

73

answers:

1

I've got an instance of a model FooBar - how do I get the naturalised type Foo Bar for display to the user, making sure to use the class's verbose_name if one is set in the Meta options. e.g.:

class FooBar(models.Model):
  title = models.CharField(max_length = 100)

  class Meta:
    verbose_name = 'Spaghetti Monster'

How do I get 'Spaghetti Monster' from an instance of a FooBar?

+5  A: 

foobar._meta.verbose_name

Ignacio Vazquez-Abrams
Won't that only work if the class has a `Meta` subclass? (See the first part of the question too).
Dominic Rodger
Models always have a `Meta` inner class, even if it's one generated implicitly by Django. And that class always has a `verbose_name` attribute. http://docs.djangoproject.com/en/dev/ref/models/options/#verbose-name
Ignacio Vazquez-Abrams
Ah - excellent - thanks!
Dominic Rodger