Django - 如何从枚举返回人类可读的(对于多复选框表单)

发布于 2025-01-11 11:27:01 字数 1104 浏览 0 评论 0原文

我有这段代码,但它只返回较短的变量值“计划 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹏雨一样淡蓝的深情 2025-01-18 11:27:01
self.get_student_loan_plan_display() # on views.py

{{object.get_student_loan_plan_display}} # on templates
self.get_student_loan_plan_display() # on views.py

{{object.get_student_loan_plan_display}} # on templates
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文