Erm. You don't seem to have specified what to order by. In this case, surely you want ... .order_by('date_time') However, this field doesn't seem to appear in your models.
One thing I've found handy for debugging Django's ORM to SQL is something which I don't think is documented in django docs at all (although I maybe wrong about that) is doing: print qs.query Where qs is your queryset. It will output the SQL Django sends to the DB backend. Euan On 3 July, 15:03, MikeHowarth <mikehowar...@googlemail.com> wrote: > Hi all > > Just coming back to Django after a long time away, and struggling to > get my head around what should be a trivial concept using the ORM. > > Essentially I want to do acheive the following SQL: > > SELECT * FROM publisher_history > INNER JOIN publisher_publisher ON publisher_publisher.id = > publisher_history.publisher_id > GROUP BY publisher_id ORDER BY date_time DESC; > > My models look like this: > > class Publisher(models.Model): > name = models.CharField(max_length=100, blank=False) > > class History(models.Model): > publisher = models.ForeignKey(Publisher, blank=False) > > I've been trying to use object values, but I'm getting duplicate > publishers. Code looks like this: > > results = History.objects.values('publisher').distinct() > > If I run: > > results = History.objects.values('publisher').distinct().order_by() > > I don't get duplicates but I don't get the results returned in the > order I expect either. > > Any help would be greatly appreciated, I've stared at this for a while > now. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.