diff --git a/django/core/serializers/xml_serializer.py b/django/core/serializers/xml_serializer.py index 9dd4eee282..988c3360e9 100644 --- a/django/core/serializers/xml_serializer.py +++ b/django/core/serializers/xml_serializer.py @@ -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. diff --git a/tests/fixtures_regress/fixtures/sequence_extra_xml.xml b/tests/fixtures_regress/fixtures/sequence_extra_xml.xml new file mode 100644 index 0000000000..dd2ee7c28f --- /dev/null +++ b/tests/fixtures_regress/fixtures/sequence_extra_xml.xml @@ -0,0 +1,10 @@ + + + + Wolf + Super Wolf + Canis lupus + 3 + 1.2 + + diff --git a/tests/fixtures_regress/tests.py b/tests/fixtures_regress/tests.py index c76056b93a..02e923e386 100644 --- a/tests/fixtures_regress/tests.py +++ b/tests/fixtures_regress/tests.py @@ -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): """