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
This commit is contained in:
Russell Keith-Magee 2007-05-28 05:41:32 +00:00
parent 3ae89e6abd
commit c84ff156c1
4 changed files with 18 additions and 2 deletions

View File

@ -37,7 +37,12 @@ class Serializer(base.Serializer):
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):

View File

@ -82,7 +82,13 @@ class Serializer(base.Serializer):
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")

View File

@ -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.

View File

@ -197,6 +197,8 @@ The end."""),
(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"),