Let's say that I have a model Foo that inherits from SuperFoo:
class SuperFoo(models.Model):
name = models.CharField('name of SuperFoo instance', max_length=50)
...
class Foo(SuperFoo):
... # do something that changes verbose_name of name field of SuperFoo
In class Foo, I'd like to override the verbose_name of the name field of SuperFoo. Can I? If not, is the best option setting a label inside the model form definition to get it displayed in a template?
Bearing in mind the caveat that modifying Foo._meta.fields will affect the superclass too - and therefore is only really useful if the superclass is abstract, I've wrapped the answer @Gerry gave up as a reusable class decorator:
Use it like this:
The example above changes the verbose_name and help_text for the inherited field 'timestamp'. You can pass in as many keyword args as there are fields you want to modify.
Your best bet would be setting/changing the label in the form itself. Referring to the
name
field of theFoo
model (eg. by looking it up inFoo._meta.fields
) will actually give you a reference to thename
field ofSuperFoo
, so changing itsverbose_name
will change it in both models.Also, adding a
name
field to theFoo
class won't work either, because...A simple hack I have used is:
Have a look at how Django-CMS does this, they override the
db_table
field in the models inheriting fromCMSPlugin
. The basics (which I also use for my own stuff) boil down to:You can add some checks, e.g. only update for subclasses (not the direct type), or only update if the value is not customized.