Question solved with such sql query:

select * from `main_home` t2, `main_states` t4, `main_person` t3
where t2.id in
  (select max(id)
  from `main_home`
  where `person_id`
  in (select `person_id` from `main_online`)
  group by `person_id`)
and t4.id=t2.`state_id`
and t3.id=t2.`person_id`

Or such expression:
ok = Home.objects.filter(id__in = [ item['id__max'] for item in
Home.objects.values('person_id').annotate(Max('id')).filter(person__in=Online.objects.all().values('person__pk'))])
ok = ok.select_related('person__name','state__state')

On 4 май, 23:46, Jacob Kaplan-Moss <ja...@jacobian.org> wrote:
> On Wed, May 4, 2011 at 9:23 AM, Satan Study Django
>
> <satan.come.and.kill.your.family.an...@gmail.com> wrote:
> > I can also use the direct sql query in the code. But on cellular it
> > level does not seem right -)
>
> Well, if it were me, I'd just use the raw SQL. I'd guess that about 5
> to 10% of the time I find that my queries are better expressed in SQL
> than in Python. I see nothing wrong with dropping down to raw SQL
> where it's easier and clearer. Why make extra work for youself?
>
> Jacob

-- 
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