我需要帮助来制定查询。
我的模型是这样:
class Rider(models.Model):
id_rider = models.IntegerField(default = 0,blank= True, primary_key=True)
first_name = models.CharField(max_length = 256,blank= True)
[...] def str (自己): 返回self.display_name
class Risultato(models.Model):
TYPE_CHOICES = (('SR','sr'),('ITT', 'itt'),('HC', 'hc'),('1C','1c'),('TTT','ttt'))
id_rider = models.ForeignKey(Rider,related_name='rider', on_delete=models.CASCADE)
id_stage = models.ForeignKey(Stage,related_name='team', on_delete=models.CASCADE)
type_ris = models.CharField(choices=TYPE_CHOICES,max_length = 256,blank= True)
rank = models.IntegerField(default = 0,blank= True)
punti = models.IntegerField(default = 0,blank= True)
def __str__(self):
return self.id_raider,self.id_stage,self.rank
我需要为骑手的细节创建一个详细视图,如下所示:
-------------------------------------------
车手:[显示名称]
年龄:[年龄]
...
...
--------得分历史-----
stage sr / itt ttt hc 1c tot
1100空1010110
2空20 5 10 35
提前谢谢!
更新
我添加了视图和模板,但看不到表格 view.py:
class RiderDetails(DetailView):
model = Rider
template_name = 'game/rider_detail.html'
def get_context_data(self, *args, **kwargs):
context_data = super().get_context_data(**kwargs)
context_data['risultato_qs'] = Risultato.objects.filter(id_rider=self.object)
return context_data
rider_detail.html:
{% extends 'game/base.html'%}
{% block content %}
<h1>{{ rider.display_name }}</h1>
<table>
<tbody>
{% for risultato in risultato_qs %}
<tr>
<td>{{ risultato.stage }}</td>
<td>{{ risutato.type_ris }}</td>
<td>{{ risutato.punti }}</td>
... etc ...
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
答案 0 :(得分:0)
一个简单的例子可能像这样:
class RiderDetails(DetailView):
model = Rider
template_name = 'path_to_rider_template'
def get_context_data(self, *args, **kwargs):
context_data = super().get_context_data(*args, **kwargs)
context_data['risultato_qs'] = Risultato.objects.filter(id_rider=self.object)
return context_data
然后在您的模板中可以,例如:
... Rider details here ...
<table>
<tbody>
{% for risultato in risultato_qs %}
<tr>
<td>{{ risultato.stage }}</td>
<td>{{ risutato.type_ris }}</td>
... etc ...
</tr>
{% endfor %}
</tbody>
</table>