我正在尝试显示选择字段的人类可读标签。
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'})
但它引发了这个错误:
AttributeError: 'Reminder' object has no attribute 'get_notification_display'
以下是模型:
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'))
以下是关于get_FOO_display
的文档 - 我缺少什么?
答案 0 :(得分:1)
对于已设置选项的每个字段,该对象将具有get_FOO_display()方法
但您没有为notification
设置任何choices。换句话说,你需要一个NOTIFICATION_CHOICES
可迭代的长度两个可迭代的类。
答案 1 :(得分:1)
notification = models.CharField(choices=provider_type, max_length=2, default='Em')
删除数组字段,因为它不允许您创建和迭代一个