49
For Django < 1.10:
model._meta.get_field_by_name('location_x')[0].verbose_name
- [Django]-Reload django object from database
- [Django]-How to disable Django's CSRF validation?
- [Django]-How to add annotate data in django-rest-framework queryset responses?
20
For Django 1.11 and 2.0:
MyModel._meta.get_field('my_field_name').verbose_name
More info in the Django doc
- [Django]-Django rest framework, use different serializers in the same ModelViewSet
- [Django]-Django Rest Framework – Get related model field in serializer
- [Django]-Django template comparing string
14
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.
- [Django]-Getting the SQL from a Django QuerySet
- [Django]-GeoDjango GEOSException error
- [Django]-Django – how to visualize signals and save overrides?
- [Django]-Django Background Task
- [Django]-Django CSRF framework cannot be disabled and is breaking my site
- [Django]-Split views.py in several files
5
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
- [Django]-Can one use the Django database layer outside of Django?
- [Django]-Why is __init__ module in django project loaded twice
- [Django]-Can a dictionary be passed to django models on create?
2
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)
- [Django]-Django – present current date and time in template
- [Django]-How do you use the django-filter package with a list of parameters?
- [Django]-Understanding ManyToMany fields in Django with a through model
0
# select fields for bulk_update : exclude primary key and relational
fieldsfields_to_update = []
for field_to_update in Model._meta.get_fields():
if not field_to_update.many_to_many and not field_to_update.many_to_one and not field_to_update.one_to_many and not field_to_update.one_to_one and not field_to_update.primary_key and not field_to_update.is_relation :
fields_to_update = fields_to_update + [field_to_update.name]
Model.objects.bulk_update(models_to_update , fields_to_update)
- [Django]-Django post_save() signal implementation
- [Django]-GeoDjango GEOSException error
- [Django]-Python vs C#/.NET — what are the key differences to consider for using one to develop a large web application?