Tuesday, August 4, 2015

Accessing a field's verbose_name

Hello

I have created models in a django application.

class FicheService(models.Model):

    id
= models.AutoField(primary_key=True)
    name
= models.CharField(max_length=64, verbose_name="Nom", blank=False)
    phone
= models.CharField(max_length=16, validators=[phone_verif], blank=True, null=True, verbose_name="N° de téléphone",
           
 help_text
="Le numéro de téléphone doit être entré dans le format:
'+999999999', jusqu'à 15 chiffres autorisés."
) # validators should be a
list

    site_web
= models.CharField(max_length=256, blank=True, null=True, verbose_name="Site web du service")
   
 commentaire
= models.CharField(max_length=128,
verbose_name
="Informations complémentaires", help_text="Facultatif",
blank
=True, null=True)

   
def __unicode__(self):
       
return self.name

   
def get_descr(self):
       
[...]

class Geomap(Page):

    services
= models.ManyToManyField(FicheService)



In the get_descr method, I would like to access the phone value (ex: +331234567), and the phone verbose_name ("N° de téléphone")...
The goal is to create a string to insert the description.

I have seen in multiple forums (including this google group : https://groups.google.com/forum/#!topic/django-users/ojaJMZ1YN-o) one answer which apparently worked for the people that asked the question, but it doesn't seem to work for me...

Here is the code I currently have for get_descr :
    def descr(self):
        my_string
= (
               
"<div class=\"info-title\">"
               
+ self.name
               
+ "</div>\n<div class=\"info-descr\">\n"
               
)
       
for curr_field in ("phone", "site_web", "commentaire"):
            curr_field_value
= getattr(self, curr_field) ;
            curr_field_name
= self._meta.get_field(curr_field).verbose_name ;
            my_string
= (
                    my_string
                   
+ "\n<br>\n"
                   
+ curr_field_name
                   
+ " : "
                   
+ curr_field_value
                   
)

        my_string
= my_string + "\n</div>"
       
return my_string



It doesn't display anything.
I have tested with the exact same code, whitout the line " + curr_field_name" and it displays the description how it should.

I also have tested to see what
self._meta.get_field("phone").verbose_name
returned, and it returns :
myapp.FicheService.phone


Does anyone have any idea about why it doesn't work for me ?

--
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/f7411bdc-afa6-4e38-b69b-92bf77f50b57%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment