Tuesday, August 4, 2015

Re: Accessing a field's verbose_name

On 4/08/2015 10:11 PM, Déborah Leder wrote:
> <https://lh3.googleusercontent.com/-kYlAklxMo5A/VcCsAaABL4I/AAAAAAAAANs/-mNIE8is27A/s1600/error_verbose_name.PNG>
>
> I am sorry, it seems like I've made a cuple of typos....

This works for me ...

def get_verbose_name(obj, field):
"""New _meta API with Django 1.8"""
return
obj.__class__()._meta.get_field('{0}'.format(field)).verbose_name

So then you can call get_verbose_name with the instance of whatever
object you want plus the actual field name to get the verbose_name
declared in the model.

Mike

>
> The name of my method is indeed get_descr all the time, so that is
> not the source of the problem.
>
> And the command that I have tested and that returns
> myapp.FicheService.phone isn't |
> self._meta.get_field("phone").verbose_name | but |
> self._meta.get_field("phone") |
>
> To be more precise, it is even : |
> unicode(self._meta.get_field("phone")) | since without unicode, it
> returns the following error (see image) :
>
> So, to sum up the lines of code and their effect : |
> self._meta.get_field("phone").verbose_name | and |
> unicode(self._meta.get_field("phone").verbose_name) | both cancel the
> display of my whole string without raising any error
>
> | self._meta.get_field("phone") | generates the error in the image
>
> | unicode(self._meta.get_field("phone")) | returns the string :
> "myapp.FicheService.phone"
>
> -- You received this message because you are subscribed to the Google
> Groups "Django users" group. To unsubscribe from this group and stop
> receiving emails from it, send an email to
> django-users+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com>. To post to this
> group, send email to django-users@googlegroups.com
> <mailto:django-users@googlegroups.com>. Visit this group at
> http://groups.google.com/group/django-users. To view this discussion
> on the web visit
> https://groups.google.com/d/msgid/django-users/a46cb96d-c204-4d42-ab2b-06090a686490%40googlegroups.com
>
> <https://groups.google.com/d/msgid/django-users/a46cb96d-c204-4d42-ab2b-06090a686490%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
>
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/55C0CC7C.3070108%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment