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('tablefield')
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 <[email protected]> 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] <
> [email protected]> 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 <[email protected]>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 [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected]<django-users%[email protected]>
>>> .
>>> 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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.