Note that there are some explanatory texts on larger screens.

plurals
  1. POGet information from related object in generic list view
    primarykey
    data
    text
    <p>So, I've been noodling about with Django's generic views, specifically the <code>object_list</code> view. I have this in my <code>urls.py</code>:</p> <pre><code>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), ) </code></pre> <p>and this fairly rough template that it is going to:</p> <pre><code>{% block content %} &lt;table&gt; &lt;tr&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Turn&lt;/th&gt; &lt;th&gt;Last Generated&lt;/th&gt; &lt;/tr&gt; {% for game in game_list %} &lt;tr&gt; &lt;td&gt;{{ game.name }}&lt;/td&gt; &lt;/tr&gt; {% endfor %} &lt;/table&gt; {% endblock %} </code></pre> <p>What I'm looking for is the best idiomatic way of including in this view the unicode representation and <code>generated</code> field (a <code>DateTimeField</code>) from the <em>most recent</em> <code>Turn</code> that points to the current <code>Game</code> in the loop, based on the value of <code>generated</code>. <code>Turn.game</code> is the field that points to the <code>Game</code> the turn belongs to (a <code>ForeignKey</code>).</p> <p><strong>Update:</strong></p> <p>My <code>Turn</code> model is as follows:</p> <pre><code>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) </code></pre> <p>The <code>Game</code> model has not appreciably changed from the way I specified it in <a href="https://stackoverflow.com/questions/1197674/actions-triggered-by-field-change-in-django">this other question</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload