我没什么问题。我有像Project和Member这样的模型。每个项目都有成员。在模板中,我想展示用户及其在特定项目中的角色。它有效,但它向我展示了角色的数量。我希望将人类可读的价值展示为"业务分析师"等等。我怎样才能接受这些价值观?
models.py:
class Project(models.Model):
members = models.ManyToManyField(User, through='Member', help_text=_('Members'))
class Member(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
role = models.CharField(max_length=20, choices=ROLE_CHOICES)
ROLE_CHOICES = (
('1', _('Manager')),
('2', _('Developer')),
('3', _('Business Analyst')),
('4', _('System Analyst')),
)
模板:
{% for member in project.member_set.all %}
<tr>
<td class="text-center">{{ member.user }}</td> <-- Example: Mark
<td class="text-center">{{ member.role }}</td> <-- Example: 3 but I need Business Analyst
</tr>
{% endfor %}
答案 0 :(得分:5)
您可以在get_FOO_display上阅读更多内容。
<td class="text-center">{{ member.get_role_display }}</td>