I have 2 django models:
class Foo(models.Model):
    baz = models.CharField()
class Bar(models.Model);
    foo = models.ForeignKey(Foo)
    qux = models.CharField()
with the following data:
Foo
id baz
4  X
5  Y
6  Z
Bar
id foo_id qux
1  4      A
2  5      A
3  5      B
4  6      B
now I do 2 queries on Bar, filtered on qux:
resA = [1, 2] (actually bar instances; shown bar.id for convenience)
resB = [3, 4] (actually bar instances; shown bar.id for convenience)
What is now the fastest way to AND these lists together so that the result will be:
resAND = [5] (foo.id)
Right now I do:
ret = []
nr_sets = 2
foos = Foo.objects.all()
bars = list(resA + resB)
for foo in foos:
    test = filter(lambda bar : bar.foo_id == foo.id, bars)
    if test == nr_sets;
        ret.append(foo)
This is however horribly slow. Any ideas in speeding this up? I am particularly looking for post query solutions, but good ideas concerning the query('s) are also welcome.
 
     
    