Skip to content

Commit

Permalink
[1.9.x] Fixed #25506 -- Allowed filtering over a RawSQL annotation.
Browse files Browse the repository at this point in the history
Co-Authored-By: Gavin Wahl <gwahl@fusionbox.com>

Backport of b971c1c from master
  • Loading branch information
acatton authored and timgraham committed Oct 14, 2015
1 parent 38d6e1e commit 36e7d27
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django/db/models/lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def process_lhs(self, compiler, connection, lhs=None):
lhs_sql = connection.ops.field_cast_sql(
db_type, field_internal_type) % lhs_sql
lhs_sql = connection.ops.lookup_cast(self.lookup_name, field_internal_type) % lhs_sql
return lhs_sql, params
return lhs_sql, list(params)

def as_sql(self, compiler, connection):
lhs_sql, params = self.process_lhs(compiler, connection)
Expand Down
2 changes: 2 additions & 0 deletions docs/releases/1.8.6.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ Bugfixes

* Fixed system check crash on ``ForeignKey`` to abstract model
(:ticket:`25503`).

* Allowed filtering over a ``RawSQL`` annotation (:ticket:`25506`).
12 changes: 12 additions & 0 deletions tests/expressions/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ def test_annotate_values_aggregate(self):
)
self.assertEqual(companies['result'], 2395)

def test_annotate_values_filter(self):
companies = Company.objects.annotate(
foo=RawSQL('%s', ['value']),
).filter(foo='value').order_by('name')
self.assertQuerysetEqual(
companies, [
'<Company: Example Inc.>',
'<Company: Foobar Ltd.>',
'<Company: Test GmbH>',
],
)

def test_filter_inter_attribute(self):
# We can filter on attribute relationships on same model obj, e.g.
# find companies where the number of employees is greater
Expand Down

0 comments on commit 36e7d27

Please sign in to comment.