> I need to be able to query an object that has a set of related
>  objects - the query is to find which objects contain the same
> set of query objects. For example the main object could be a
> research paper and the related object would be a set of topics
> that the papers could contain.


This is a popular question that I've found a solution to.  It 
might be kinder in the queryset refactor (Malcolm?), but for the 
time being, this is what I've used:

http://groups.google.com/group/django-users/browse_thread/thread/24de9d4b74935296/

Because of the way the underlying SQL builds the query, you 
basically end up asking for two contradictory conditions of the 
same row:

   WHERE x = 1 AND x = 2

which, as you discovered, returns no rows.

The solution I've found is to use an extra() call to build an 
IN/EXISTS[1] subselect that asks what I want.  Thus, it becomes 
something like

   WHERE EXISTS(
     SELECT 0
     FROM <innertbl> i
     WHERE <condition1> AND
       i.<fk> = <outertbl>.id
     ) AND EXISTS(
     SELECT 0
     FROM <innertbl> i
     WHERE <condition2> AND
       i.<fk> = <outertbl>.id
     )

-tim

[1] FWIW, I've found EXISTS to be faster in PostgreSQL and IN to 
be faster in SQLServer; haven't profiled MySQL or sqlite.






--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to