Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #19998 -- Fixed --ignorenonexistent support for XML based fixtu…

…res.
  • Loading branch information...
commit 465b01f065a8482bfa42c19e3854a3c810e69d33 1 parent 6c730da
@chsigi chsigi authored apollo13 committed
View
6 django/core/serializers/xml_serializer.py
@@ -155,6 +155,7 @@ def __init__(self, stream_or_string, **options):
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 @@ def _handle_object(self, node):
# {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 @@ def _handle_object(self, node):
# 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.
View
10 tests/fixtures_regress/fixtures/sequence_extra_xml.xml
@@ -0,0 +1,10 @@
+<?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>
View
14 tests/fixtures_regress/tests.py
@@ -87,6 +87,20 @@ def test_loaddata_not_found_fields_ignore(self):
)
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):
"""
Please sign in to comment.
Something went wrong with that request. Please try again.