On Tuesday, May 24, 2011 1:51:30 PM UTC+1, Gchorn wrote:
>
> Hi All, 
>
> I'm trying to create a website that aggregates information about the 
> various products made by different companies.  To that end, I have 
> models (classes) for companies, products, and events.  Since each 
> company has various products, each product is related to one or more 
> companies by a ManyToManyKey.  Each product has various news events, 
> so each event is related to a specific product by ForeignKey as well. 
>
> I've used Django's render_to_response shortcut to send a specific 
> company as a context to a template, in order to render a webpage that 
> lists all of the products made by that specific company in a table. 
> Each row in the table is occupied by a different product, and each 
> column in the table displays a specific piece of info about that 
> product.  I want the last column to display the most recent news event 
> object for each product, but I can't figure out how to filter for 
> this.  I currently have the template set to look like this (I'm 
> intentionally leaving out some syntax to keep it simple): 
>
> for product in company.product_set.all: 
> <td>{{product.name}} 
> <td>{{product.usage}} 
> <td>blah blah blah 
> <td>{{product.event_set.all}} 
>
> And that last line is where I'm stuck.  I want to somehow filter 
> "product.event_set.all" for the most recent event associated with that 
> product, but I'm not sure how to do that and I can't find it in the 
> documentation on the Django website.  One of the attributes of the 
> "event" class I created is the event date; is there any way to filter 
> for the most recent date? 
>
> Furthermore, I'd like to make a link out of that latest event that 
> will take you to the full story when you click on it.  (I am setting 
> up the website to have a webpage for each event object, in which the 
> full story for the event is displayed.  Each "full-story" page will be 
> indexed by the id of the event object in question.)  I know the basic 
> format of this should be: 
>
> <td><a href='events/{{event.id}}/'>{{product.event_set.all| 
> something}} 
>
> But I don't know how to set the "id" variable from within the template 
> so that you're automatically taken to the correct page, and I can't 
> think of how to set this variable (or context) from the views.py file, 
> since its value changes for each product listed in the table. 
>
> Can anyone help me with this? 
>
> cheers, 
> Gchorn


What you really want to do is to call a method on the Product model which 
returns the latest event. Actually, there's a built-in queryset function for 
this, so you just need to give it the field to order by, which you can do by 
setting `get_latest_by = 'event_date'` (or whatever the field name is) in 
the Event's inner Meta class.

Then, in the template, you can call that method:

    {% with product.event_set.latest as latest_event %}
        <a href="{% url event latest_event.id %}">{{ latest_event.name 
}}</a>
    {% endwith %}

A couple of things there - I'm using `with`, as you reference the event 
twice so that'll avoid two separate db hits; and you should always use the 
{% url %} tag to look up URLs against your urlconf, rather than hard-coding 
them.
--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to