Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get access to a Django Model field verbose name dynamically?

Tags:

field

django

get

I'd like to have access to one my model field verbose_name.

I can get it by the field indice like this

model._meta._fields()[2].verbose_name

but I need to get it dynamically. Ideally it would be something like this

model._meta._fields()['location_x'].verbose_name

I've looked at a few things but I just can't find it.

like image 230
philgo20 Avatar asked Mar 11 '10 22:03

philgo20


People also ask

Why do we use verbose in Django?

verbose_name is a human-readable name for the field. If the verbose name isn't given, Django will automatically create it using the field's attribute name, converting underscores to spaces. This attribute in general changes the field name in admin interface.

What is Verbose_name_plural in Django?

The verbose_name_plural [Django-doc] is a human readable name you give to the objects. You can use this in forms, dialogs, and other means to communicate with the user. There are no naming conventions (like PEP-8 for example), since this deals with communication between the app and the user.

Does Django create ID field?

By default, Django adds an id field to each model, which is used as the primary key for that model. You can create your own primary key field by adding the keyword arg primary_key=True to a field. If you add your own primary key field, the automatic one will not be added.


7 Answers

For Django < 1.10:

model._meta.get_field_by_name('location_x')[0].verbose_name
like image 134
Ignacio Vazquez-Abrams Avatar answered Oct 03 '22 04:10

Ignacio Vazquez-Abrams


model._meta.get_field('location_x').verbose_name

like image 34
Bertrand Bordage Avatar answered Oct 03 '22 04:10

Bertrand Bordage


For Django 1.11 and 2.0:

MyModel._meta.get_field('my_field_name').verbose_name

More info in the Django doc

like image 22
renno Avatar answered Oct 03 '22 04:10

renno


The selected answer gives a proxy object which might look as below.

<django.utils.functional.__proxy__ object at 0x{SomeMemoryLocation}>

If anyone is seeing the same, you can find the string for the verbose name in the title() member function of the proxy object.

model._meta.get_field_by_name(header)[0].verbose_name.title()

A better way to write this would be:

model._meta.get_field(header).verbose_name.title()

where header will be the name of the field you are interested in. i.e., 'location-x' in OPs context.

NOTE: Developers of Django also feel that using get_field is better and thus have depreciated get_field_by_name in Django 1.10. Thus I would suggest using get_field no matter what version of Django you use.

like image 27
Nithish Avatar answered Oct 03 '22 05:10

Nithish


model._meta.get_field_by_name('location_x')[0].verbose_name
like image 41
Dmitry Shevchenko Avatar answered Oct 03 '22 03:10

Dmitry Shevchenko


You can also use:

Model.location_x.field.verbose_name

Model being the class name. I tested this on my Animal model:

Animal.sale_price.field.verbose_name

Animal.sale_price returns a DeferredAttribute, which has several meta data, like the verbose_name

Note: I'm using Django 3.1.5

like image 24
Alvaro Rodriguez Scelza Avatar answered Oct 03 '22 05:10

Alvaro Rodriguez Scelza


If you want to iterate on all the fields you need to get the field:

for f in BotUser._meta.get_fields():
    if hasattr(f, 'verbose_name'):
        print(f.verbose_name)
like image 40
Arian Soltani Avatar answered Oct 03 '22 03:10

Arian Soltani