From 3fef1f4c8d779ab40a13f13a1829a98e48e58c9c Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Fri, 20 Jul 2007 12:07:58 +0000 Subject: [PATCH] Fixed #4558 -- Modified XML serializer to handle whitespace better around None tags. Thanks to Bill Fenner for the report and fix. git-svn-id: http://code.djangoproject.com/svn/django/trunk@5727 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/serializers/xml_serializer.py | 4 ++-- .../fixtures_regress/fixtures/pretty.xml | 11 +++++++++++ .../regressiontests/fixtures_regress/models.py | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 tests/regressiontests/fixtures_regress/fixtures/pretty.xml diff --git a/django/core/serializers/xml_serializer.py b/django/core/serializers/xml_serializer.py index 50bce929b3..667faf7c5e 100644 --- a/django/core/serializers/xml_serializer.py +++ b/django/core/serializers/xml_serializer.py @@ -171,7 +171,7 @@ class Deserializer(base.Deserializer): elif field.rel and isinstance(field.rel, models.ManyToOneRel): data[field.attname] = self._handle_fk_field_node(field_node, field) else: - if len(field_node.childNodes) == 1 and field_node.childNodes[0].nodeName == 'None': + if field_node.getElementsByTagName('None'): value = None else: value = field.to_python(getInnerText(field_node).strip()) @@ -185,7 +185,7 @@ class Deserializer(base.Deserializer): Handle a node for a ForeignKey """ # Check if there is a child node named 'None', returning None if so. - if len(node.childNodes) == 1 and node.childNodes[0].nodeName == 'None': + if node.getElementsByTagName('None'): return None else: return field.rel.to._meta.get_field(field.rel.field_name).to_python( diff --git a/tests/regressiontests/fixtures_regress/fixtures/pretty.xml b/tests/regressiontests/fixtures_regress/fixtures/pretty.xml new file mode 100644 index 0000000000..68e5710c6a --- /dev/null +++ b/tests/regressiontests/fixtures_regress/fixtures/pretty.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/tests/regressiontests/fixtures_regress/models.py b/tests/regressiontests/fixtures_regress/models.py index 31528238e9..41ef8c26bf 100644 --- a/tests/regressiontests/fixtures_regress/models.py +++ b/tests/regressiontests/fixtures_regress/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.contrib.auth.models import User class Animal(models.Model): name = models.CharField(maxlength=150) @@ -14,6 +15,13 @@ class Plant(models.Model): # For testing when upper case letter in app name; regression for #4057 db_table = "Fixtures_regress_plant" +class Stuff(models.Model): + name = models.CharField(maxlength=20, null=True) + owner = models.ForeignKey(User, null=True) + + def __unicode__(self): + return unicode(self.name) + u' is owned by ' + unicode(self.owner) + __test__ = {'API_TESTS':""" >>> from django.core import management @@ -26,4 +34,13 @@ __test__ = {'API_TESTS':""" >>> animal = Animal(name='Platypus', latin_name='Ornithorhynchus anatinus') >>> animal.save() +############################################### +# Regression test for ticket #4558 -- pretty printing of XML fixtures +# doesn't affect parsing of None values. + +# Load a pretty-printed XML fixture with Nulls. +>>> management.load_data(['pretty.xml'], verbosity=0) +>>> Stuff.objects.all() +[] + """}