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:
parent
3ae89e6abd
commit
c84ff156c1
|
@ -37,6 +37,11 @@ class Serializer(base.Serializer):
|
|||
def handle_fk_field(self, obj, field):
|
||||
related = getattr(obj, field.name)
|
||||
if related is not None:
|
||||
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
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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"),
|
||||
|
|
Loading…
Reference in New Issue