diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 0b5e7d47829..e2f8c6c48ed 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -385,6 +385,7 @@ class Field(object): """ 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'): diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py index 1fb50376256..432984f6bbd 100644 --- a/tests/model_fields/tests.py +++ b/tests/model_fields/tests.py @@ -494,3 +494,21 @@ class GenericIPAddressFieldTests(test.TestCase): model_field = models.GenericIPAddressField(protocol='IPv6') form_field = model_field.formfield() self.assertRaises(ValidationError, form_field.clean, '127.0.0.1') + + +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)