Ticket #14786: 14786_fix_with_tests.diff

File 14786_fix_with_tests.diff, 2.8 KB (added by Aramgutang, 4 years ago)

Fix with a regression test.

  • django/db/models/fields/__init__.py

     
    304304        "Returns field's value prepared for database lookup."
    305305        if not prepared:
    306306            value = self.get_prep_lookup(lookup_type, value)
     307            prepared = True
    307308        if hasattr(value, 'get_compiler'):
    308309            value = value.get_compiler(connection=connection)
    309310        if hasattr(value, 'as_sql') or hasattr(value, '_as_sql'):
  • tests/regressiontests/model_fields/tests.py

     
    88from django.db.models.fields.files import FieldFile
    99from django.utils import unittest
    1010
    11 from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel, Document
     11from models import (Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post,
     12                    NullBooleanModel, BooleanModel, Document, Base64TextField)
    1213
    1314# If PIL available, do these tests.
    1415if Image:
     
    348349        field = d._meta.get_field('myfile')
    349350        field.save_form_data(d, 'else.txt')
    350351        self.assertEqual(d.myfile, 'else.txt')
     352
     353class CustomFieldTests(unittest.TestCase):
     354    def test_14786(self):
     355        """
     356        Test that field values are not prepped twice.
     357        """
     358        # Regression test for #14786. When get_db_prep_lookup is called with
     359        # prepared = False, the value is passed through get_prep_value twice.
     360        from django.db import connection
     361        self.assertEqual(
     362            Base64TextField().get_db_prep_lookup('exact',
     363                                                 'test',
     364                                                 connection=connection,
     365                                                 prepared=False),
     366            ['dGVzdA==\n'])
     367
  • tests/regressiontests/model_fields/models.py

     
    158158                                  width_field='headshot_width')
    159159
    160160###############################################################################
     161# Custom field
     162
     163class Base64TextField(models.TextField):
     164    def get_prep_value(self, value):
     165        if value is None:
     166            return value
     167        return super(Base64TextField, self).get_prep_value(value).encode('base64')
     168
     169
     170###############################################################################
Back to Top