Commit 465b01f0 authored by Christoph Sieghart's avatar Christoph Sieghart Committed by Florian Apolloner
Browse files

Fixed #19998 -- Fixed --ignorenonexistent support for XML based fixtures.

parent 6c730da1
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -155,6 +155,7 @@ class Deserializer(base.Deserializer):
        super(Deserializer, self).__init__(stream_or_string, **options)
        self.event_stream = pulldom.parse(self.stream, self._make_parser())
        self.db = options.pop('using', DEFAULT_DB_ALIAS)
        self.ignore = options.pop('ignorenonexistent', False)

    def _make_parser(self):
        """Create a hardened XML parser (no custom/external entities)."""
@@ -188,6 +189,7 @@ class Deserializer(base.Deserializer):
        # {m2m_accessor_attribute : [list_of_related_objects]})
        m2m_data = {}

        model_fields = Model._meta.get_all_field_names()
        # Deseralize each field.
        for field_node in node.getElementsByTagName("field"):
            # If the field is missing the name attribute, bail (are you
@@ -198,7 +200,9 @@ class Deserializer(base.Deserializer):

            # Get the field from the Model. This will raise a
            # FieldDoesNotExist if, well, the field doesn't exist, which will
            # be propagated correctly.
            # be propagated correctly unless ignorenonexistent=True is used.
            if self.ignore and field_name not in model_fields:
                continue
            field = Model._meta.get_field(field_name)

            # As is usually the case, relation fields get the special treatment.
+10 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<django-objects version="1.0">
    <object pk="1" model="fixtures_regress.animal">
        <field type="CharField" name="name">Wolf</field>
        <field type="CharField" name="extra_name">Super Wolf</field>
        <field type="CharField" name="latin_name">Canis lupus</field>
        <field type="IntegerField" name="count">3</field>
        <field type="FloatField" name="weight">1.2</field>
    </object>
</django-objects>
+14 −0
Original line number Diff line number Diff line
@@ -87,6 +87,20 @@ class TestFixtures(TestCase):
        )
        self.assertEqual(Animal.specimens.all()[0].name, 'Lion')

    def test_loaddata_not_found_fields_ignore_xml(self):
        """
        Test for ticket #19998 -- Ignore entries in the XML serialised data
        for fields that have been removed from the model definition.
        """
        management.call_command(
            'loaddata',
            'sequence_extra_xml',
            ignore=True,
            verbosity=0,
            commit=False
        )
        self.assertEqual(Animal.specimens.all()[0].name, 'Wolf')

    @skipIfDBFeature('interprets_empty_strings_as_nulls')
    def test_pretty_print_xml(self):
        """