<snip>A perfect explanation</snip> > > s1 = set([obj.id for obj in > item1.sub_item_set.filter(options__name__exact="Small")]) > s2 = set([obj.id for obj in > item1.sub_item_set.filter(options__name__exact="Blue")]) > > smallAndBlueIDs = s1.intersection(s2) > smallAndBlueSubItems = Sub_Item.objects.in_bulk(list(smallAndBlueIDs)) > > You are just doing some of the work in SQL (the two filter clauses) and > then manually intersecting the IDs of the results and reloading the > objects by the resulting IDs. > > But, yuck! And it hits the DB 3 times instead of only once. > > David - Yes, you hit the nail on the head perfectly. I had the sinking suspicion I was trying to do something with SQL that was pretty difficult (or at least challenging for me with my limited SQL skills). It's interesting that you came up with the set approach. I decided that I knew how I could do this in Python with sets so I did something similar.
I greated a function in Items called (get_sub_items) that takes a set of values then calls another sub_item function (get_sub_values) that returns a set of the current options. It's pretty easy then to loop through and find the sub_item I'm looking for and use set equality to make sure order of the values doesn't matter. I have some concerns about the number of db queries but it's probably not any better or worse than the solution you outlined. I think I'll stick to it for a while and see how it works. I really appreciate that you showed me this wasn't some simple thing that I was just overlooking. Like they say, "premature optimization is the root of all evil!" Thanks, Chris --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

