Permalink
Browse files

Fixed #20348 -- Consistently handle Promise objects in model fields.

All Promise objects were passed to force_text() deep in ORM query code.
Not only does this make it difficult or impossible for developers to
prevent or alter this behaviour, but it is also wrong for non-text
fields.

This commit changes `Field.get_prep_value()` from a no-op to one that
resolved Promise objects. All subclasses now call super() method first
to ensure that they have a real value to work with.
  • Loading branch information...
1 parent 8f5533a commit 31e6d58d46894ca35080b4eab7967e4c6aae82d4 Tai Lee committed with akaariai May 3, 2013
@@ -148,6 +148,7 @@ def get_prep_value(self, value):
value properly, and preserve any other lookup parameters before
returning to the caller.
"""
+ value = super(GeometryField, self).get_prep_value(value)
if isinstance(value, SQLEvaluator):
return value
elif isinstance(value, (tuple, list)):
@@ -17,7 +17,7 @@
from django.core import exceptions, validators
from django.utils.datastructures import DictWrapper
from django.utils.dateparse import parse_date, parse_datetime, parse_time
-from django.utils.functional import curry, total_ordering
+from django.utils.functional import curry, total_ordering, Promise
from django.utils.text import capfirst
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
@@ -421,6 +421,8 @@ def get_prep_value(self, value):
"""
Perform preliminary non-db specific value checks and conversions.
"""
+ if isinstance(value, Promise):
+ value = value._proxy____cast()
return value
def get_db_prep_value(self, value, connection, prepared=False):
@@ -704,6 +706,7 @@ def get_db_prep_value(self, value, connection, prepared=False):
return value
def get_prep_value(self, value):
+ value = super(AutoField, self).get_prep_value(value)
if value is None:
return None
return int(value)
@@ -763,6 +766,7 @@ def get_prep_lookup(self, lookup_type, value):
return super(BooleanField, self).get_prep_lookup(lookup_type, value)
def get_prep_value(self, value):
+ value = super(BooleanField, self).get_prep_value(value)
if value is None:
return None
return bool(value)
@@ -796,6 +800,7 @@ def to_python(self, value):
return smart_text(value)
def get_prep_value(self, value):
+ value = super(CharField, self).get_prep_value(value)
return self.to_python(value)
def formfield(self, **kwargs):
@@ -911,6 +916,7 @@ def get_prep_lookup(self, lookup_type, value):
return super(DateField, self).get_prep_lookup(lookup_type, value)
def get_prep_value(self, value):
+ value = super(DateField, self).get_prep_value(value)
return self.to_python(value)
def get_db_prep_value(self, value, connection, prepared=False):
@@ -1008,6 +1014,7 @@ def pre_save(self, model_instance, add):
# get_prep_lookup is inherited from DateField
def get_prep_value(self, value):
+ value = super(DateTimeField, self).get_prep_value(value)
value = self.to_python(value)
if value is not None and settings.USE_TZ and timezone.is_naive(value):
# For backwards compatibility, interpret naive datetimes in local
@@ -1096,6 +1103,7 @@ def get_db_prep_save(self, value, connection):
self.max_digits, self.decimal_places)
def get_prep_value(self, value):
+ value = super(DecimalField, self).get_prep_value(value)
return self.to_python(value)
def formfield(self, **kwargs):
@@ -1185,6 +1193,7 @@ class FloatField(Field):
description = _("Floating point number")
def get_prep_value(self, value):
+ value = super(FloatField, self).get_prep_value(value)
if value is None:
return None
return float(value)
@@ -1218,6 +1227,7 @@ class IntegerField(Field):
description = _("Integer")
def get_prep_value(self, value):
+ value = super(IntegerField, self).get_prep_value(value)
if value is None:
return None
return int(value)
@@ -1326,6 +1336,7 @@ def get_db_prep_value(self, value, connection, prepared=False):
return value or None
def get_prep_value(self, value):
+ value = super(GenericIPAddressField, self).get_prep_value(value)
if value and ':' in value:
try:
return clean_ipv6_address(value, self.unpack_ipv4)
@@ -1391,6 +1402,7 @@ def get_prep_lookup(self, lookup_type, value):
value)
def get_prep_value(self, value):
+ value = super(NullBooleanField, self).get_prep_value(value)
if value is None:
return None
return bool(value)
@@ -1473,6 +1485,7 @@ def get_internal_type(self):
return "TextField"
def get_prep_value(self, value):
+ value = super(TextField, self).get_prep_value(value)
if isinstance(value, six.string_types) or value is None:
return value
return smart_text(value)
@@ -1549,6 +1562,7 @@ def pre_save(self, model_instance, add):
return super(TimeField, self).pre_save(model_instance, add)
def get_prep_value(self, value):
+ value = super(TimeField, self).get_prep_value(value)
return self.to_python(value)
def get_db_prep_value(self, value, connection, prepared=False):
@@ -253,6 +253,7 @@ def get_prep_lookup(self, lookup_type, value):
def get_prep_value(self, value):
"Returns field's value prepared for saving into a database."
+ value = super(FileField, self).get_prep_value(value)
# Need to convert File objects provided via a form to unicode for database insertion
if value is None:
return None
@@ -11,8 +11,6 @@
from django.db.models.sql.datastructures import Date, DateTime
from django.db.models.sql.query import Query
from django.db.models.sql.where import AND, Constraint
-from django.utils.functional import Promise
-from django.utils.encoding import force_text
from django.utils import six
from django.utils import timezone
@@ -147,10 +145,6 @@ def add_update_fields(self, values_seq):
Used by add_update_values() as well as the "fast" update path when
saving models.
"""
- # Check that no Promise object passes to the query. Refs #10498.
- values_seq = [(value[0], value[1], force_text(value[2]))
- if isinstance(value[2], Promise) else value
- for value in values_seq]
self.values.extend(values_seq)
def add_related_update(self, model, field, value):
@@ -210,12 +204,6 @@ def insert_values(self, fields, objs, raw=False):
into the query, for example.
"""
self.fields = fields
- # Check that no Promise object reaches the DB. Refs #10498.
- for field in fields:
- for obj in objs:
- value = getattr(obj, field.attname)
- if isinstance(value, Promise):
- setattr(obj, field.attname, force_text(value))
self.objs = objs
self.raw = raw
Oops, something went wrong.

0 comments on commit 31e6d58

Please sign in to comment.