Reputation: 3008
I am trying to display human readable label of choicefield.
def reminder(request, pk=None):
reminder = get_object_or_404(Reminder, pk=pk)
print(reminder.id) # printing correct value
print(reminder.get_notification_display())
return render(request, 'reminder.html', {'reminder': reminder, 'title': 'Reminder'})
But it throws this error :
AttributeError: 'Reminder' object has no attribute 'get_notification_display'
Here is the model:
class Reminder(models.Model):
provider_type = (
('Em', 'Email'),
('Sm', 'SMS'),
('De', 'Desktop'),
('Mo', 'Mobile'),
)
notification = ArrayField(models.CharField(choices=provider_type, max_length=2, default='Em'))
Here's the documentation on get_FOO_display
- what am I missing?
Upvotes: 1
Views: 777
Reputation: 27503
notification = models.CharField(choices=provider_type, max_length=2, default='Em')
remove the arrayfield as it is not allowing you to create and iterate one
Upvotes: 1
Reputation: 78650
The documentation says
For every field that has choices set, the object will have a get_FOO_display() method
but you don't have any choices set for notification
. In other words, you need a NOTIFICATION_CHOICES
iterable of lenght two iterables in your class.
Upvotes: 1