how to override the verbose name of a superclass model field in django
Solution 1
A simple hack I have used is:
class SuperFoo(models.Model):
name = models.CharField('name of SuperFoo instance', max_length=50)
...
class Meta:
abstract = True
class Foo(SuperFoo):
... # do something that changes verbose_name of name field of SuperFoo
Foo._meta.get_field('name').verbose_name = 'Whatever'
Solution 2
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:
def modify_fields(**kwargs):
def wrap(cls):
for field, prop_dict in kwargs.items():
for prop, val in prop_dict.items():
setattr(cls._meta.get_field(field), prop, val)
return cls
return wrap
Use it like this:
@modify_fields(timestamp={
'verbose_name': 'Available From',
'help_text': 'Earliest date you can book this'})
class Purchase(BaseOrderItem):
pass
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.
Solution 3
Your best bet would be setting/changing the label in the form itself. Referring to the name
field of the Foo
model (eg. by looking it up in Foo._meta.fields
) will actually give you a reference to the name
field of SuperFoo
, so changing its verbose_name
will change it in both models.
Also, adding a name
field to the Foo
class won't work either, because...
Overriding fields in a parent model leads to difficulties in areas such as initialising new instances (specifying which field is being intialised in
Model.__init__
) and serialization. These are features which normal Python class inheritance doesn't have to deal with in quite the same way, so the difference between Django model inheritance and Python class inheritance isn't merely arbitrary.
Solution 4
Have a look at how Django-CMS does this, they override the db_table
field in the models inheriting from CMSPlugin
. The basics (which I also use for my own stuff) boil down to:
class SuperFooMetaClass(ModelBase):
def __new__(cls, name, bases, attrs):
new_class = super(SuperFooMetaClass, cls).__new__(cls, name, bases, attrs)
new_class._meta.verbose_name = "...." # perhaps only if not customized
return new_class
class SuperFoo(models.Model):
__metaclass__ = SuperFooMetaClass
....
You can add some checks, e.g. only update for subclasses (not the direct type), or only update if the value is not customized.
Solution 5
I have different child classes deriving from the same base class. And I only care for the form view in the Django Admin interface.
The only solution that worked for me was to customize the Django Admin interface and set the verbose_name (or help_text) there:
class FooAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
form = super().get_form(request, obj, **kwargs)
form.base_fields['name'].verbose_name = "my verbose name"
return form
admin.site.register(models.Foo, FooAdmin)
shanyu
Mechanical Engineering + MBA + PhD in finance. Python & Javascript.
Updated on June 07, 2022Comments
-
shanyu about 2 years
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?
-
ebuat3989 over 10 yearsThis actually changes the verbose_name of the field for the SuperFoo class. For example, if you have: class Bar(SuperFoo), the verbose_name for the 'name' field is also 'Whatever'... even if you follow the same procedure with Bar._meta.get_field('name').verbose_name = 'Whatever 2'
-
ebuat3989 over 10 yearsIf SuperFoo is an abstract base class, the above comment is not true. This method works well as long as the SuperFoo class is abstract, so class Meta: abstract = True
-
Sassan over 9 yearsThe problem is sometimes SuperFoo can't be abstract
-
emi almost 7 yearsThanks! Worked like a charm on Django 1.11.4 .