Wednesday, June 29, 2011

getting objects unique in several ways (annotate?)

Sorry about the vague title. Its hard for me to summarize this in one
short sentence.
Lets say I have two models (using pseudo code):

name = charfield()

start_date = DateTimeField()
item = ManytoMany(Book)
recipient = CharField()

Now for every book, I want to know the recipient of only the most
recent transaction. I can get the start date of the most recent
transaction for each book with

Book.objects.annotate(mostrecent = Max('transaction__start_date'))

but what I would like is the pk of that transaction, so I can get more
information about it. Can this be done even in multiple queries?
Must I hack a solution using lists instead of queries?

I have simplified my question quite a bit, I hope not too much.

thanks for reading,

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment