On 29 Sty, 03:16, marsanyi <[email protected]> wrote:
> The joy of annotate(). In Django 1.1 and above:
>
> result = Student.objects.annotate(latest = models.Max('grades__date'))
>
> gives you the recordset, sans test_name. Any advances on this?
>
I would suggest:
students = Student.objects.annotate(latest_grade_pk=models.Max
('grades__pk'))
latest_grades = Grade.objects.filter(pk__in=[st.latest_grade_pk for st
in students])
Filtering on dates can choke on two identical dates.
More on this:
http://stackoverflow.com/questions/2074514/django-query-that-get-most-recent-objects-from-different-categories/2076665
--
Tomasz Zielinski
http://pyconsultant.eu
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.