Fixed #4288 -- Modified serializers to pay attention to the to_field attribute on ForeignKeys. Thanks to Sandro Dentella for the report and the helpful test case.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5232 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
f15036ae6d
commit
c38a93e4d9
|
@ -1404,21 +1404,24 @@ def load_data(fixture_labels, verbosity=1):
|
||||||
if verbosity > 1:
|
if verbosity > 1:
|
||||||
print "No %s fixture '%s' in %s." % \
|
print "No %s fixture '%s' in %s." % \
|
||||||
(format, fixture_name, humanize(fixture_dir))
|
(format, fixture_name, humanize(fixture_dir))
|
||||||
if count[0] == 0:
|
|
||||||
if verbosity > 0:
|
|
||||||
print "No fixtures found."
|
|
||||||
else:
|
|
||||||
if verbosity > 0:
|
|
||||||
print "Installed %d object(s) from %d fixture(s)" % tuple(count)
|
|
||||||
sequence_sql = backend.get_sql_sequence_reset(style, models)
|
sequence_sql = backend.get_sql_sequence_reset(style, models)
|
||||||
if sequence_sql:
|
if sequence_sql:
|
||||||
if verbosity > 1:
|
if verbosity > 1:
|
||||||
print "Resetting sequences"
|
print "Resetting sequences"
|
||||||
for line in sequence_sql:
|
for line in sequence_sql:
|
||||||
cursor.execute(line)
|
cursor.execute(line)
|
||||||
|
|
||||||
transaction.commit()
|
transaction.commit()
|
||||||
transaction.leave_transaction_management()
|
transaction.leave_transaction_management()
|
||||||
|
|
||||||
|
if count[0] == 0:
|
||||||
|
if verbosity > 0:
|
||||||
|
print "No fixtures found."
|
||||||
|
else:
|
||||||
|
if verbosity > 0:
|
||||||
|
print "Installed %d object(s) from %d fixture(s)" % tuple(count)
|
||||||
|
|
||||||
load_data.help_doc = 'Installs the named fixture(s) in the database'
|
load_data.help_doc = 'Installs the named fixture(s) in the database'
|
||||||
load_data.args = "[--verbosity] fixture, fixture, ..."
|
load_data.args = "[--verbosity] fixture, fixture, ..."
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,7 @@ class Serializer(base.Serializer):
|
||||||
def handle_fk_field(self, obj, field):
|
def handle_fk_field(self, obj, field):
|
||||||
related = getattr(obj, field.name)
|
related = getattr(obj, field.name)
|
||||||
if related is not None:
|
if related is not None:
|
||||||
related = related._get_pk_val()
|
related = getattr(related, field.rel.field_name)
|
||||||
self._current[field.name] = related
|
self._current[field.name] = related
|
||||||
|
|
||||||
def handle_m2m_field(self, obj, field):
|
def handle_m2m_field(self, obj, field):
|
||||||
|
@ -80,7 +80,10 @@ def Deserializer(object_list, **options):
|
||||||
|
|
||||||
# Handle FK fields
|
# Handle FK fields
|
||||||
elif field.rel and isinstance(field.rel, models.ManyToOneRel):
|
elif field.rel and isinstance(field.rel, models.ManyToOneRel):
|
||||||
data[field.attname] = field.rel.to._meta.pk.to_python(field_value)
|
if field_value:
|
||||||
|
data[field.attname] = field.rel.to._meta.get_field(field.rel.field_name).to_python(field_value)
|
||||||
|
else:
|
||||||
|
data[field.attname] = None
|
||||||
|
|
||||||
# Handle all other fields
|
# Handle all other fields
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -82,7 +82,7 @@ class Serializer(base.Serializer):
|
||||||
self._start_relational_field(field)
|
self._start_relational_field(field)
|
||||||
related = getattr(obj, field.name)
|
related = getattr(obj, field.name)
|
||||||
if related is not None:
|
if related is not None:
|
||||||
self.xml.characters(str(related._get_pk_val()))
|
self.xml.characters(str(getattr(related, field.rel.field_name)))
|
||||||
else:
|
else:
|
||||||
self.xml.addQuickElement("None")
|
self.xml.addQuickElement("None")
|
||||||
self.xml.endElement("field")
|
self.xml.endElement("field")
|
||||||
|
@ -181,7 +181,7 @@ class Deserializer(base.Deserializer):
|
||||||
if len(node.childNodes) == 1 and node.childNodes[0].nodeName == 'None':
|
if len(node.childNodes) == 1 and node.childNodes[0].nodeName == 'None':
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
return field.rel.to._meta.pk.to_python(
|
return field.rel.to._meta.get_field(field.rel.field_name).to_python(
|
||||||
getInnerText(node).strip().encode(self.encoding))
|
getInnerText(node).strip().encode(self.encoding))
|
||||||
|
|
||||||
def _handle_m2m_field_node(self, node, field):
|
def _handle_m2m_field_node(self, node, field):
|
||||||
|
|
|
@ -116,6 +116,16 @@ class FKSelfData(models.Model):
|
||||||
class M2MSelfData(models.Model):
|
class M2MSelfData(models.Model):
|
||||||
data = models.ManyToManyField('self', null=True, symmetrical=False)
|
data = models.ManyToManyField('self', null=True, symmetrical=False)
|
||||||
|
|
||||||
|
|
||||||
|
class UniqueAnchor(models.Model):
|
||||||
|
"""This is a model that can be used as
|
||||||
|
something for other models to point at"""
|
||||||
|
|
||||||
|
data = models.CharField(unique=True, maxlength=30)
|
||||||
|
|
||||||
|
class FKDataToField(models.Model):
|
||||||
|
data = models.ForeignKey(UniqueAnchor, null=True, to_field='data')
|
||||||
|
|
||||||
# The following test classes are for validating the
|
# The following test classes are for validating the
|
||||||
# deserialization of objects that use a user-defined
|
# deserialization of objects that use a user-defined
|
||||||
# field as the primary key.
|
# field as the primary key.
|
||||||
|
|
|
@ -184,6 +184,12 @@ The end."""),
|
||||||
(m2m_obj, 445, M2MSelfData, []),
|
(m2m_obj, 445, M2MSelfData, []),
|
||||||
(m2m_obj, 446, M2MSelfData, []),
|
(m2m_obj, 446, M2MSelfData, []),
|
||||||
|
|
||||||
|
(data_obj, 450, UniqueAnchor, "UAnchor 1"),
|
||||||
|
(fk_obj, 451, FKDataToField, "UAnchor 1"),
|
||||||
|
(fk_obj, 452, FKDataToField, "UAnchor 2"),
|
||||||
|
(fk_obj, 453, FKDataToField, None),
|
||||||
|
(data_obj, 454, UniqueAnchor, "UAnchor 2"),
|
||||||
|
|
||||||
(data_obj, 500, Anchor, "Anchor 3"),
|
(data_obj, 500, Anchor, "Anchor 3"),
|
||||||
(data_obj, 501, Anchor, "Anchor 4"),
|
(data_obj, 501, Anchor, "Anchor 4"),
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue