Skip to content

Commit

Permalink
Fixed #14786 -- Fixed get_db_prep_lookup calling get_prep_value twice…
Browse files Browse the repository at this point in the history
… if prepared is False.

Thanks homm for the report and Aramgutang and lrekucki for work on
the patch.
  • Loading branch information
timgraham committed Aug 29, 2013
1 parent af953c4 commit f19a366
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions django/db/models/fields/__init__.py
Expand Up @@ -499,6 +499,7 @@ def get_db_prep_lookup(self, lookup_type, value, connection,
"""
if not prepared:
value = self.get_prep_lookup(lookup_type, value)
prepared = True
if hasattr(value, 'get_compiler'):
value = value.get_compiler(connection=connection)
if hasattr(value, 'as_sql') or hasattr(value, '_as_sql'):
Expand Down
18 changes: 18 additions & 0 deletions tests/model_fields/tests.py
Expand Up @@ -663,3 +663,21 @@ def test_URLField(self):
self.assertIsInstance(
URLField().get_prep_value(lazy_func()),
six.text_type)


class CustomFieldTests(unittest.TestCase):

def test_14786(self):
"""
Regression test for #14786 -- Test that field values are not prepared
twice in get_db_prep_lookup().
"""
prepare_count = [0]
class NoopField(models.TextField):
def get_prep_value(self, value):
prepare_count[0] += 1
return super(NoopField, self).get_prep_value(value)

field = NoopField()
field.get_db_prep_lookup('exact', 'TEST', connection=connection, prepared=False)
self.assertEqual(prepare_count[0], 1)

0 comments on commit f19a366

Please sign in to comment.