0

This is how my model looks like:

class FacilityHoursOfOperation(models.Model):
    DAYS_OF_WEEK = (
        (0, 'Monday'),
        (1, 'Tuesday'),
        (2, 'Wednesday'),
        (3, 'Thursday'),
        (4, 'Friday'),
        (5, 'Saturday'),
        (6, 'Sunday'),
    )

    day = models.CharField(max_length=1, null=True, choices=DAYS_OF_WEEK, verbose_name="Day")

This is how my view looks like:

hoursofoperationslist = FacilityHoursOfOperation.objects.filter(some filter)

return render(request, template_name="sometemplate.html",context={'hoursofoperation':hoursofoperationslist})

This is how my template looks like:

{% for hop in hoursofoperation %}
   <tr>
      <td>{{ hop.get_day_display }}</td>
   </tr>
{% endfor %}

I was expecting it to display "Monday" or "Tuesday" etc. instead of 0 or 1. But it display 0 or 1.

Looking at the documentation at https://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.get_FOO_display, it looks like it is supposed to but it is not.

What am I missing here?

Asdfg
  • 11,362
  • 24
  • 98
  • 175

1 Answers1

1

You are using a CharField but the keys in your CHOICES are integers. Either change them to strings - '0', '1' etc - or change the field to an IntegerField.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895