Django - 如何从枚举返回人类可读的(对于多复选框表单)
我有这段代码,但它只返回较短的变量值“计划 1”、“计划 2”,而不是我在模型中定义的较长的人类可读形式。
我通过调用并传入变量
(correct instance).student_loan_plan.all(),
并通过“渲染”函数中的“上下文”变量传递该变量,
从 form.py 和 view.py 中生成 HTML 模板中的复选框模型
# U.K. STUDENT LOAN PLAN MODEL
class UK_Student_Loan_Plan(models.Model):
STUDENT_LOAN_PLANS = [
('Plan 1', 'Plan 1: English or Welsh student'),
('Plan 2', 'Plan 2: English or Welsh who started on 1 September 2012'),
('Plan 4', 'Plan 4: Scottish student who started on 1 September 1998'),
('Postgraduate', 'Postgraduate: English or Welsh student'),
]
student_loan_plan_id = models.BigAutoField(verbose_name='Student Loan Plan ID', primary_key=True, serialize=False, auto_created=True)
student_loan_plan = models.CharField(verbose_name='Student Loan Plan', max_length=12, choices=STUDENT_LOAN_PLANS, blank=True, unique=True, null=True)
def __str__(self):
return self.student_loan_plan
问题
Q1。如何返回完整的人类可读表单?
我已经尝试过,但没有成功
def __str__(self):
return self.student_loan_plan.choice
I have this code, but it only returns the shorter variable values, 'Plan 1', 'Plan 2', not their longer human-readable forms which I have defined in the model.
I generate checkboxes in a HTML template from a form.py and view.py by calling and passing in the variable
(correct instance).student_loan_plan.all(),
and passing that through the 'context' variable in the 'render' function
MODEL
# U.K. STUDENT LOAN PLAN MODEL
class UK_Student_Loan_Plan(models.Model):
STUDENT_LOAN_PLANS = [
('Plan 1', 'Plan 1: English or Welsh student'),
('Plan 2', 'Plan 2: English or Welsh who started on 1 September 2012'),
('Plan 4', 'Plan 4: Scottish student who started on 1 September 1998'),
('Postgraduate', 'Postgraduate: English or Welsh student'),
]
student_loan_plan_id = models.BigAutoField(verbose_name='Student Loan Plan ID', primary_key=True, serialize=False, auto_created=True)
student_loan_plan = models.CharField(verbose_name='Student Loan Plan', max_length=12, choices=STUDENT_LOAN_PLANS, blank=True, unique=True, null=True)
def __str__(self):
return self.student_loan_plan
QUESTION
Q1. How do I get the full human readable form returned?
I've tried this, but no success
def __str__(self):
return self.student_loan_plan.choice
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)