Using field__in with a QuerySet results in undesired behaviour of johnny

Issue #48 resolved
created an issue




queryset2 = Model1.objects.all() result = Model2.objects.filter(id__in = queryset2.values_list("id", flat=True)) }}}

With johnny's QuerySetCache enabled won't behave as expected. To make this work with johnny the QuerySet used in the "in" parameter must be cast to a list to evaluate it in place: {{{


result = Model2.filter(id__in = list(queryset2.values_list("id", flat=True))) }}}

This evaluates the QuerySet and does the actual query (or not, if cached by johnny) instead of "injecting" the SQL query of the QuerySet as a SQL subquery into the main queryset SQL query.

Using the first version roughly this SQL would be executed: {{{


SELECT ... FROM "table" WHERE "table"."field" IN (SELECT U0."id" FROM "table2" U0 WHERE ...) }}}

Completely messing up the cache invalidation, as the parameters for the QuerySet, for example a list of IDs, would never be honored by the key generator. Though this may be expected behaviour it would be nice to at least point to it in the documentation or somehow honor the parameters of the "in" parameter in the key generation it's a QuerySet.

Comments (7)

  1. Jason Moiron repo owner

    The version of johnny in hg-tip doesn't seem to fall victim to this behavior on Django 1.3 or Django 1.4. The test case is SingleModelTest.test_in_values_list; I made sure that the subquery is actually a ValuesListQuerySet. if you're still experiencing this, please fork and add a failing test or come up with a failing test using johnny.tests.testapp (or a simple set of custom models).

  2. Log in to comment