我有一个名为Season的模型和一个名为Game的模型:
# Season
class Season(models.Model):
teams = models.ManyToManyField('Team', related_name='season_teams', blank=True)
current = models.BooleanField(default=False)
year = models.PositiveIntegerField(
validators=[
MinValueValidator(2018),
MaxValueValidator(datetime.datetime.now().year)],
help_text="Use the following format: <YYYY>", null=True, blank=True)
session = models.CharField(max_length=100, null=True, blank=True)
class Meta:
ordering = ['year', 'session']
def __str__(self):
session = self.session
year = str(self.year)
season = session + " " + year
return season
# Game
class Game(models.Model):
field_choices = FIELD_CHOICES
team_choices = TEAM_CHOICES
season = models.ForeignKey(Season, on_delete=models.CASCADE)
home_team = models.CharField(max_length=100, null=True, blank=True, choices=team_choices)
away_team = models.CharField(max_length=100, null=True, blank=True, choices=team_choices)
field = models.CharField(max_length=100, choices=field_choices, null=True)
date = models.DateTimeField(null=True, blank=True)
score = models.CharField(max_length=5, null=False, blank=True, default='')
tbd = 'TBD'
win = 'W'
draw = 'D'
loss = 'L'
result_choices = (
(tbd, 'TBD'),
(win, 'W'),
(draw, 'D'),
(loss, 'L'),
)
result = models.CharField(
max_length=3,
choices=result_choices,
default=tbd,
)
class Meta:
ordering = ['home_team', 'away_team', 'field','score', 'result']
def __str__(self):
return str(self.date)
def __unicode__(self):
return u'%s' % self.name
我有一个视图季节成功查询两个模型:
# Season
class Season(generic.ListView):
model = SeasonModel
template_name = 'team/season.html'
def get_queryset(self):
qs1 = SeasonModel.objects.filter(current=True)
qs2 = GameModel.objects.all().order_by('date')
queryset1 = sorted(chain(qs1))
queryset2 = sorted(chain(qs2),key=attrgetter('home_team'))
result = queryset1 + queryset2
return result
然后是一个模板,该模板应该呈现团队的日程安排,但它会使用一个额外的空白行呈现它,就好像我创建了一个具有空白属性的Game实例。这是我的模板:
<div id="schedule_header">
<h5 id="datetime">Today's date is {% now "DATE_FORMAT" %}</h5>
{% for season in object_list %}
{% if season.current %}
<a href=""><h5 id="session">{{ season.year }} {{ season.session }} session</h5></a>
<a href=""><h5 id="season_history">Past Seasons</h5></a>
{% endif %}
{% endfor %}
</div>
<table class="table" id="e_schedule">
<thead>
<tr>
<th id="thead"><h2>Week</h2></th>
<th id="thead"><h2>Matchup</h2></th>
<th id="thead"><h2>Field</h2></th>
<th id="thead"><h2>Date/time</h2></th>
<th id="thead"><h2>Score</h2></th>
<th id="thead"><h2>Result</h2></th>
</tr>
</thead>
<tbody>
{% for game in object_list %}
<tr>
<th id="counter"><p>{{ forloop.counter }}</p></th>
<th id="matchup"><p>{{ game.home_team }} vs. {{ game.away_team }}</p></th>
<th id="field"><p>{{ game.get_field_display }}</p></th>
<th id="date"><p>{{ game.date.date }} at {{ game.date.time }}</p></th>
<th id="score"><p>{{ game.score }}</p></th>
<th id="result"><p>{{ game.result }}</p></th>
</tr>
{% endfor %}
</tbody>
</table>
所以我的问题是如何在不在表格中添加额外空行的情况下执行此操作?我猜它与我在视图中使用的链itertool有关,因为当我只对Game做一个简单的查询时,这不会发生。
答案 0 :(得分:0)
我认为问题是因为你组合了两个不同的查询集。 变量可以通过上下文发送:
class Season(generic.ListView):
model = SeasonModel
template_name = 'team/season.html'
def get_queryset(self):
return SeasonModel.objects.filter(current=True)
def get_context_data(self,**kwargs):
context = super(Season,self).get_context_data(**kwargs)
context['games_list'] = GameModel.objects.all().order_by('date')
return context
在你的HTML中,你有两个不同的变量
{% for season in object_list %}
<a href=""><h5 id="session">{{ season.year }} {{ season.session }} session</h5></a>
'''
{% endfor %}
对于你有games_list
{% for game in games_list %}
<tr>
'''
</tr>
{% endfor %}