On Wed, 2007-10-31 at 16:46 -0700, Andy Brody wrote: > Hi all, > > I'm using an intermediate table of Activities to relate Musicians, > Instruments, and Groups. Is there a good way to select groups from the > musician's activities? I've been doing stuff like [act.group for act > in theMusician.activity_set.all()]. This works pretty well, except > that there are duplicates if a musician plays more than one instrument > in a group, and merely doing set( the_above ) doesn't get rid of them > even though group1==group2 evaluates to True. > > Is there some better way to do this? It seems strange to be trying to > remove the duplicates at this level rather than at the SQL level with > a distinct(). The resulting SQL (which I don't want to write myself)
Well, there's the first problem. If Django's ORM can't do something -- we aren't trying to completely replicate SQL, after all, so there will always be some cases that can't be done in Python -- then writing custom SQL is not just possible, it's recommended. > should be something like: > > SELECT DISTINCT * > FROM `app_group` > WHERE `id` > IN ( > > SELECT `group_id` > FROM `app_activity` > WHERE `musician_id` = 1 > ) Ideally, you want to be able to write something involving .values(...).distinct(). Unfortunately, referencing related model fields via values() isn't possible yet. It will be possible when the queryset-refactor work is completed (this is ticket #5768). Regards, Malcolm -- The early bird may get the worm, but the second mouse gets the cheese. http://www.pointy-stick.com/blog/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---