Skip to content

Commit

Permalink
Fixed #24320 - Used field.value_to_string() in serialization of forei…
Browse files Browse the repository at this point in the history
…gn key.

This fixes serialization of a ForeignKey to a UUIDField as the
test indicates.
  • Loading branch information
coldmind authored and timgraham committed Feb 13, 2015
1 parent b4caa91 commit 5c995dc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 2 additions & 0 deletions django/core/serializers/python.py
Expand Up @@ -63,6 +63,8 @@ def handle_fk_field(self, obj, field):
value = None
else:
value = getattr(obj, field.get_attname())
if not is_protected_type(value):
value = field.value_to_string(obj)
self._current[field.name] = value

def handle_m2m_field(self, obj, field):
Expand Down
4 changes: 4 additions & 0 deletions tests/serializers_regress/models.py
Expand Up @@ -276,6 +276,10 @@ class UUIDData(models.Model):
data = models.UUIDField(primary_key=True)


class FKToUUID(models.Model):
data = models.ForeignKey(UUIDData)


class ComplexModel(models.Model):
field1 = models.CharField(max_length=10)
field2 = models.CharField(max_length=10)
Expand Down
22 changes: 12 additions & 10 deletions tests/serializers_regress/tests.py
Expand Up @@ -28,15 +28,15 @@
BooleanData, BooleanPKData, CharData, CharPKData, ComplexModel, DateData,
DateTimeData, DecimalData, DecimalPKData, EmailData, EmailPKData,
ExplicitInheritBaseModel, FileData, FilePathData, FilePathPKData, FKData,
FKDataNaturalKey, FKDataToField, FKDataToO2O, FKSelfData, FloatData,
FloatPKData, GenericData, GenericIPAddressData, GenericIPAddressPKData,
InheritAbstractModel, InheritBaseModel, IntegerData, IntegerPKData,
Intermediate, LengthModel, M2MData, M2MIntermediateData, M2MSelfData,
ModifyingSaveData, NaturalKeyAnchor, NullBooleanData, O2OData,
PositiveIntegerData, PositiveIntegerPKData, PositiveSmallIntegerData,
PositiveSmallIntegerPKData, ProxyBaseModel, ProxyProxyBaseModel, SlugData,
SlugPKData, SmallData, SmallPKData, Tag, TextData, TimeData, UniqueAnchor,
UUIDData,
FKDataNaturalKey, FKDataToField, FKDataToO2O, FKSelfData, FKToUUID,
FloatData, FloatPKData, GenericData, GenericIPAddressData,
GenericIPAddressPKData, InheritAbstractModel, InheritBaseModel,
IntegerData, IntegerPKData, Intermediate, LengthModel, M2MData,
M2MIntermediateData, M2MSelfData, ModifyingSaveData, NaturalKeyAnchor,
NullBooleanData, O2OData, PositiveIntegerData, PositiveIntegerPKData,
PositiveSmallIntegerData, PositiveSmallIntegerPKData, ProxyBaseModel,
ProxyProxyBaseModel, SlugData, SlugPKData, SmallData, SmallPKData, Tag,
TextData, TimeData, UniqueAnchor, UUIDData,
)

try:
Expand Down Expand Up @@ -201,6 +201,7 @@ def inherited_compare(testcase, pk, klass, data):
o2o_obj = (o2o_create, o2o_compare)
pk_obj = (pk_create, pk_compare)
inherited_obj = (inherited_create, inherited_compare)
uuid_obj = uuid.uuid4()

This comment has been minimized.

Copy link
@mjtamlyn

mjtamlyn Feb 14, 2015

Member

This will be a little confusing for future work - it has the same name format as the above tuples but does not follow the comment.


test_data = [
# Format: (data type, PK value, Model Class, data)
Expand Down Expand Up @@ -356,7 +357,8 @@ def inherited_compare(testcase, pk, klass, data):
# The end."""),
# (pk_obj, 770, TimePKData, datetime.time(10, 42, 37)),
# (pk_obj, 790, XMLPKData, "<foo></foo>"),
(pk_obj, 791, UUIDData, uuid.uuid4()),
(pk_obj, 791, UUIDData, uuid_obj),
(fk_obj, 792, FKToUUID, uuid_obj),

(data_obj, 800, AutoNowDateTimeData, datetime.datetime(2006, 6, 16, 10, 42, 37)),
(data_obj, 810, ModifyingSaveData, 42),
Expand Down

0 comments on commit 5c995dc

Please sign in to comment.