So, I've been noodling about with Django's generic views, specifically the object_list
view. I have this in my urls.py
:
from django.conf.urls.defaults import *
from django.views.generic import list_detail
from diplomacy.engine.models import Game
game_info = {
"queryset": Game.objects.filter(state__in=('A', 'P')),
"template_object_name": "game",
}
urlpatterns = patterns('',
(r'^$', list_detail.object_list, game_info),
)
and this fairly rough template that it is going to:
{% block content %}
<table>
<tr>
<th>Name</th>
<th>Turn</th>
<th>Last Generated</th>
</tr>
{% for game in game_list %}
<tr>
<td>{{ game.name }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
What I'm looking for is the best idiomatic way of including in this view the unicode representation and generated
field (a DateTimeField
) from the most recent Turn
that points to the current Game
in the loop, based on the value of generated
. Turn.game
is the field that points to the Game
the turn belongs to (a ForeignKey
).
Update:
My Turn
model is as follows:
SEASON_CHOICES = (
('S', 'Spring'),
('SR', 'Spring Retreat'),
('F', 'Fall'),
('FR', 'Fall Retreat'),
('FB', 'Fall Build')
)
class Turn(models.Model):
game = models.ForeignKey(Game)
year = models.PositiveIntegerField()
season = models.CharField(max_length=2, choices=SEASON_CHOICES)
generated = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return "%s %s" % (self.season, self.year)
The Game
model has not appreciably changed from the way I specified it in this other question.