> The two arrays are just normal dictionaries that look something like
> this:
> 
> friends = [id=1,id=2,id=3]
> friends2 = [id=3]

This is the first suspicious item because if it is a dictionary, 
the key "id" appears more than once -- something that can't 
happen with a dict.

Sets are the usual tool for this, and until friend relationships 
exceed tens-of-thousands (where the number of IDs makes for an 
unwieldy query or list stashed in memory), should be usable:

   me = Person.objects.get(my_id)
   my_friends = me.friend_set
   my_friends_ids = set(friend.id
     for friend
     in my_friends)

   other_person = Person.objects.get(other_id)
   their_friends = other_person.friend_set
   their_friend_ids = set(friend.id
     for friend
     in their_friends)

   # your magic happens here:
   common_friend_ids =  my_friends_ids.intersection(
     their_friend_ids)

   # if you want more than their IDs, you have to ask for them
   common_friends = Person.objects.in_bulk(
     tuple(common_friend_ids))

(that in_bulk() call doesn't seem to like using a set() for its 
source of IDs, only allowing list/tuple rather than arbitrary 
iterables)

It might also be possible to do this via the extra() call to add 
your own funky sifting on the DB side.  This could end up being 
considerably faster -- but I'd have to think longer about how to 
do that one, so I'll leave it as an exercise to the reader.  ;-)

-tim




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

Reply via email to