Re: Comparing two ManyToMany fields for same entries

2011-10-29 Thread Kevin
Your version isn't as bad you think, here is the SQL it generates: SELECT "relations_person"."id", "relations_person"."name" FROM "relations_person" INNER JOIN "relations_person_friends" ON ("relations_person"."id" = "relations_person_friends"."to_person_id") WHERE "relations_person_friends

Re: Comparing two ManyToMany fields for same entries

2011-10-28 Thread Brett Epps
I'm going to guess that my version is slow since a new query would be run for each row returned by the initial one. Check out an app called django-debug-toolbar. It has a view that will show you all of the SQL queries generated as a page is built. It will also tell you how long each query took t

Re: Comparing two ManyToMany fields for same entries

2011-10-28 Thread Kevin
I think the filter I used does work to detect related friends, here is a Pythonic example of the same code, but done differently without filters: Person.objects.get(name='Person1').friends.get().friends.all() Instead of returning back 'Person1', when using filters, this returns back 'Person0'. P

Re: Comparing two ManyToMany fields for same entries

2011-10-28 Thread Kevin
Thanks. Is this way good for database optimization though? I guess I could use select_related(). I was hoping there was a QuerySet I could run. I made this one, although I'm not sure if it works correctly: Person.objects.filter(name='Person1').filter(friends__friends__name='Person0') This outp

Re: Comparing two ManyToMany fields for same entries

2011-10-28 Thread Brett Epps
Try this: for friendof0 in Person0.friends.all(): for friendof1 in Person1.friends.all(): if friendof0 == friendof1: # Person 0 and Person 1 share a friend. else: # They have no shared friends. Brett On 10/28/11 12:59 PM, "Kevin" wrote: >Just though

Re: Comparing two ManyToMany fields for same entries

2011-10-28 Thread Kevin
Just thought I'd add another example using Python script: Person0 = Person() Person1 = Person() Person2 = Person() Person0.friends.add(Person2) Person2.friends.add(Person0) Person2.friends.add(Person1) Person1.friends.add(Person2) Now, I would like to do the following, but it seems to fail: Pers