Here's something you could do. Execute the raw SQL to bring back just the id's, then whilst iterating over that, if you need to grab the ORM'd row, just do "row_orm = model.get(id=iterated_id_here)"
This is what I tend to do for raw sql queries. On Wed, Jan 19, 2011 at 11:26 AM, [CPR]-AL.exe <cpr.al....@gmail.com> wrote: > Hmm, yeah, but that still iterates over the objects and filters out > them by count on the application server, not on the database one :( > > I'm bypassing it already in my running project, but I try to avoid > using raw sql as longer as it is possible. So, the question, actually > remains open. > > It should go something like [pseudo-code]: > > > Book.objects.values('tablefield').annotate(Count('tablefield')).filter('tablefield__count__gte > = 1) > > But, of course, that wouldn't work, because annotate's return value is > not a queryset :( > > On 19 янв, 14:18, "Cal Leeming [Simplicity Media Ltd]" > <cal.leem...@simplicitymedialtd.co.uk> wrote: > > Yeah you should be able to use annotate, something like: > > > > from django.db.models import Count > > res = > > > Book.objects.values('tablefield').annotate(Count('tablefield')).order_by('t > ablefield') > > res2 = filter(lambda x: x.tablefield__count > 1, res) > > > > The above is probably not going to work first time, but it would be > > something along those lines most likely. > > > > I would suggest that for complex queries, bypassing the ORM isn't > > necessarily a bad thing, and there are many cases where a developer will > > purposely bypass the ORM at bottlenecks and directly query SQL for > > optimization. > > > > > > > > > > > > > > > > On Wed, Jan 19, 2011 at 11:07 AM, [CPR]-AL.exe <cpr.al....@gmail.com> > wrote: > > > Umm... Don't know, actually. That seems, that it would be okay, too. Is > > > there a way to execute this one using ORM? > > > > > On Wed, Jan 19, 2011 at 2:04 PM, Cal Leeming [Simplicity Media Ltd] < > > > cal.leem...@simplicitymedialtd.co.uk> wrote: > > > > >> May I ask why you didn't just use: > > > > >> SELECT id from table GROUP BY tablefield HAVING (COUNT(tablefield) > > 1) > > > > >> On Wed, Jan 19, 2011 at 11:02 AM, [CPR]-AL.exe <cpr.al....@gmail.com > >wrote: > > > > >>> Hi there. > > > > >>> I'm trying to do something like this: > > > > >>> SELECT * > > >>> FROM table > > >>> WHERE tablefield IN ( > > >>> SELECT tablefield > > >>> FROM table > > >>> GROUP BY tablefield > > >>> HAVING (COUNT(tablefield ) > 1) > > >>> ) > > > > >>> Tried it in many ways, but didn't suceed. Is there a way to do it > with > > >>> Django ORM without having to iterate over objects or using raw SQL? > > > > >>> -- > > >>> 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<django-users%2bunsubscr...@googlegroups.com> > <django-users%2Bunsubscribe@google groups.com> > > >>> . > > >>> For more options, visit this group at > > >>>http://groups.google.com/group/django-users?hl=en. > > > > > -- > > > Sincerely yours, Alexey. > > -- > 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<django-users%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- 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.