我有这个型号:
class ShipClass (models.Model):
ALLEGIENCE_CHOICES = (
('FED', 'Federation'),
('KGE', 'Klingon Empire'),
('RSE', 'Romulan Star Empire')
)
Origin = models.CharField(max_length=3, choices=ALLEGIENCE_CHOICES, default='FED')
ClassName = models.CharField(max_length=20)
NumberOfCrew = models.IntegerField()
def __str__(self):
return self.ClassName
class Meta:
unique_together = ("Origin", "ClassName")
现在,我在视图中有这个:
def shipclasses(request):
qs = models.ShipClass.objects.order_by("Origin", "NumberOfCrew")
return render_to_response('fleet/templates/Ship_Classes.html', {'qs' : qs})
Origin是三个字母的代码。有没有办法让我可以从视图中的ALLEGIENCE_CHOICES获取“origin”字段的全文,以便我可以在模板上显示它?
由于
答案 0 :(得分:0)
那是Model.get_FOO_display()
querySearch = ShipClass.objects.order_by("Origin", "NumberOfCrew")
querySearch.get_origin_display()
现在你将得到联邦' ...
答案 1 :(得分:0)
def shipclasses(request):
qs = models.ShipClass.objects.order_by("Origin", "NumberOfCrew")
return render_to_response('fleet/templates/Ship_Classes.html', {'qs' : qs})
无需更改您的观点。只需在模板中执行:
{% for q in qs %}
{{ q.get_origin_diplay }}
{% endfor %}