Thursday, June 30, 2011

Re: getting objects unique in several ways (annotate?)

On Jun 30, 1:31 am, elliot <> wrote:
> 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):
> Book(model.Model):
>   name = charfield()
> Transaction(model.Model):
>   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.  

Sorry no time to find out and provide a working anwser, but anyway:
you may want to read the doc for QuerySet.extra. Also, sometimes it's
just a matter of starting from the other side of the relationship...


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