Get information from related object in generic list view

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.

-------------Problems Reply------------

If Turn.game points to the associated Game object, then {{game.turn_set.all}} should return the set of Turn objects for that game.

You may need to add a Meta class to the Turn model to order from newest to oldest.

Class Meta:
ordering = ['-generated']

Then, {{game.turn_set.all.0}} should return the unicode representation for the newest turn for that game, and {{game.turn_set.all.0.generated}} will return the associated datetime object.

Note: This is untested code.

Category:python Views:0 Time:2009-08-13

Related post

  • How to aggregate related object counts in a view effectively? 2011-08-17

    Imagine this scenario: I have three tables in my database: Products, Users and Likes, the latter representing a relation between a Product and a User. Now I have a query, which joins Products with the Likes table, counting how many Likes a Product go

  • Manipulate object after generic 'create_object' view (python/django) 2012-01-24

    Is it possible to manually add field entries to an object that has been created with the generic view? Eg, from django.views.generic.create_update import create_object def create_thing(request): queryset = Things.objects.all() #Somehow pass something

  • Problem of creating new generic related object inside admin generic inline forms 2009-06-02

    I have strange problem with admin generic inline forms. I have two models, main Project and Video with ManyToMany relation trough VideoLink, becouse I need to be able linking different number of Video to Project and many project to Video: class Video

  • Duplicating model instances and their related objects in Django / Algorithm for recusrively duplicating an object 2009-01-12

    I've models for Books, Chapters and Pages. They are all written by a User: from django.db import models class Book(models.Model) author = models.ForeignKey('auth.User') class Chapter(models.Model) author = models.ForeignKey('auth.User') book = models

  • How do you access Class object for generics? 2009-01-19

    How can I access Class object for Generics? Currently, I am doing it this way: List<String> list= new ArrayList<String>(); list.getClass(); Is this OK? Or, what should be the way? --------------Solutions------------- That will return the

  • Caching Django query results for __unicode__ calls that refer to related objects 2009-12-20

    I have the following models: class Territory(models.Model): name = models.CharField(max_length=30) power = models.ForeignKey(Power, null=True, blank=True) is_supply = models.BooleanField() class Subregion(models.Model): territory = models.ForeignKey(

  • Django GenericRelation doesn't save related object's id - is this a bug or am I doing it wrong? 2011-01-02

    I have a model with a generic relation (call it A), when creating an instance of this object I pass an instance of another model (call it B) as the initializer of the content_object field (via kwargs of the constructor). If I don't save B before crea

  • Entity Framework: Attaching related objects and other state management 2011-07-06

    I have an application which uses Entity Framework Code First. I am attempting to write my resource access layer. I have several objects which all have separate database tables and a lot of object relationships. Can someone point me to an up-to-date e

  • ChangeTracker Entity Framework 4.1 - Original Values of Related Objects 2011-08-16

    I have a base class that I inherit from that has two zero to many relationships with other entities: public abstract class WebObject { public WebObject() { RelatedTags = new List<Tag>(); RelatedWebObjects = new List<WebObject>(); } [Key,

  • django order_with_respect_to with related objects 2012-01-03

    I am working on a django application that contains a bunch of related objects. I have test objects, each with an ordered set of questions (each question has a correct_answer property). And I also have test attempt objects, which are related to test o

  • Hydrate related objects 2012-01-05

    I am looking for a simple way to hydrate a related object. A Note belongs to a Document and only owners of a Document can add Notes so when a user tries to edit a Note, I need to hydrate the related Document in order to find out if the user has acces

  • Django models - related objects validation 2012-03-14

    I'm wondering how to have validations for related objects. To my surprise I haven't found much relevant information on this. For example: class Listing(models.Model): categories = models.ManyToManyField('Category') price_sale = models.DecimalField(ma

  • How to relate objects with schema.org which are on different pages? 2012-04-02

    I am searching for a solution for my problem for days and I have still no Idea if it's possible or not. I want to relate objects/things with schema.org and microdata notation like with RDFa and the about/rel attributes. Example: Page 1: www.mypage.co

  • How can I write a method within a Django model to retrieve related objects? 2008-10-21

    I have two models. We'll call them object A and object B. Their design looks something like this: class Foo(models.Model): name = models.CharField() class Bar(models.Model): title = models.CharField() Foo= models.ForeignKey('myapp.Foo') Now, suppose

  • How do I implement a common interface for Django related object sets? 2009-02-05

    Here's the deal: I got two db models, let's say ShoppingCart and Order. Following the DRY principle I'd like to extract some common props/methods into a shared interface ItemContainer. Everything went fine till I came across the _flush() method which

  • EF insert related objects 2009-03-14

    I am using silverlight client with ado.net dataservices on entity framework. I have an Entity Contact and an Entity Address which is related with a foreign key relation ship... A contact can have 1 or more Adresses but a Address needs always at least

  • Entity Framework - Detach and keep related object graph 2009-07-21

    I've just started using Entity Framework rather than my normal NHiberante to see how EF works and so far I'm having lots of problems but one in particular is detaching an object and keeping the related child objects. I bought the O'Reilly Entity Fram

  • Closing a django admin add related object popup window on save 2009-10-09

    In the django admin, when a user successfully saves (after my clean method) a new or changed related object which was edited in a popup, I'd like the popup to close instead of going to a different view. I believe I could use response_change or respon

  • Are we unable to use Interop objects in generic objects in .NET 4.0? 2009-11-19

    I'm working in VS 2010 and working on upgrading our application to .NET 4. The application is build with Excel as the base and we want to take advantage of some of the improvements to .NET for using Excel. But I ran across a strange error that seems

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.169 (s). 11 q(s)