Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed a serialization problem with objects that have a foreign key on…

… an object whose primary key is a OneToOneField.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c84ff156c190d7e425da1abef56f09127fd7e9db 1 parent 3ae89e6
Russell Keith-Magee freakboy3742 authored
7 django/core/serializers/python.py
View
@@ -37,7 +37,12 @@ def handle_field(self, obj, field):
def handle_fk_field(self, obj, field):
related = getattr(obj, field.name)
if related is not None:
- related = getattr(related, field.rel.field_name)
+ if field.rel.field_name == related._meta.pk.name:
+ # Related to remote object via primary key
+ related = related._get_pk_val()
+ else:
+ # Related to remote object via other field
+ related = getattr(related, field.rel.field_name)
self._current[field.name] = related
def handle_m2m_field(self, obj, field):
8 django/core/serializers/xml_serializer.py
View
@@ -82,7 +82,13 @@ def handle_fk_field(self, obj, field):
self._start_relational_field(field)
related = getattr(obj, field.name)
if related is not None:
- self.xml.characters(str(getattr(related, field.rel.field_name)))
+ if field.rel.field_name == related._meta.pk.name:
+ # Related to remote object via primary key
+ related = related._get_pk_val()
+ else:
+ # Related to remote object via other field
+ related = getattr(related, field.rel.field_name)
+ self.xml.characters(str(related))
else:
self.xml.addQuickElement("None")
self.xml.endElement("field")
3  tests/regressiontests/serializers_regress/models.py
View
@@ -129,6 +129,9 @@ class M2MSelfData(models.Model):
class FKDataToField(models.Model):
data = models.ForeignKey(UniqueAnchor, null=True, to_field='data')
+class FKDataToO2O(models.Model):
+ data = models.ForeignKey(O2OData, null=True)
+
# The following test classes are for validating the
# deserialization of objects that use a user-defined
# field as the primary key.
2  tests/regressiontests/serializers_regress/tests.py
View
@@ -197,6 +197,8 @@ def pk_compare(testcase, pk, klass, data):
(fk_obj, 451, FKDataToField, "UAnchor 2"),
(fk_obj, 452, FKDataToField, None),
+ (fk_obj, 460, FKDataToO2O, 300),
+
(data_obj, 500, Anchor, "Anchor 3"),
(data_obj, 501, Anchor, "Anchor 4"),
(data_obj, 502, UniqueAnchor, "UAnchor 2"),
Please sign in to comment.
Something went wrong with that request. Please try again.