Search code examples
djangotemplatesdjango-templatesformattingdetailview

Django DetailView Template show get_FIELDNAME_display() values of all fields


I made up a template to show all the fields and values from a model, it looks like this:

## detail_template.html ##
<html>
<body>
{% for name, value in object.get_fields %}
    <p>
        <label>{% trans name %}:</label>
        <span>{{ value|default:"Not available" }}</span>
    </p>
{% endfor %}
</body>
<html>

In the class you can see the fields and functions declared:

## models.py ##
Class Object:
    GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
    )    

    name = models.CharField(verbose_name=u"Full name", max_length=200, blank=True, null=True)
    sex =   models.CharField(verbose_name=u"Sex", max_length=1, choices=GENDER_CHOICES)
    birthdate = models.DateField(verbose_name=u"Birth date", help_text="Format: DD/MM/AAAA", blank=True, null=True)
    status = models.IntegerField(verbose_name=u"Status", db_index=True, default=1)
    #...tons of other fields...

    def get_status_display(self):
        if self.status == 1:
            return "Active"
        else if self.status == 0:
            return "Inactive"
        else:
            return "Dead"

    def get_birthdate_display(self):
        return self.birthdate.strftime("%d/%m/%Y")

    def get_fields(self):
        return [(field.verbose_name, self._get_FIELD_display(field)) for field in self.__class__._meta.fields]

*What is already displaying is:
Full name: Not available (because I left it blank)
Sex: Male (instead of M)
Birth date: April 10, 2012 (NOT in the format returned by get_birthdate_display)
Status: 3 (NOT the string returned in get_status_display)


*What I want to display is:
Full name: Not available (because I left it blank)
Sex: Male (instead of M)
Birth date: 20/10/1952 (with the format in the get_birthdate_display)
Status: Dead (the string returned in get_status_display)


So I want to get the get_FIELDNAME_display when there is one without doing manually something like:

<p>
    <label>{% trans 'Full name' %}:</label>
    <span>{{ object.name|default:"Not available" }}</span>
</p>    
<p>
    <label>{% trans 'Sex' %}:</label>
    <span>{{ object.get_sex_display|default:"Not available" }}</span>
</p>
<p>
    <label>{% trans 'Birth Date' %}:</label>
    <span>{{ object.get_birthdate_display|default:"Not available" }}</span>
</p>
<p>
    <label>{% trans 'Status' %}:</label>
    <span>{{ object.get_status_display|default:"Not available" }}</span>
</p>

I appreciate any help! Tks


Solution

  • Not particularly beautiful, but you could change the get_fields method in your model to something like:

    def get_fields(self):
        pairs = []
        for field in self._meta.fields:
            name = field.name
            try:
                pairs.append((name, getattr(self, "get_%s_display" % name)()))
            except AttributeError:
                pairs.append((name, getattr(self, name)))
        return pairs