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

This commit is contained in:
Christoph Sieghart 2013-03-06 22:15:36 +01:00 committed by Florian Apolloner
parent 6c730da1f6
commit 465b01f065
3 changed files with 29 additions and 1 deletions

View File

@ -155,6 +155,7 @@ class Deserializer(base.Deserializer):
super(Deserializer, self).__init__(stream_or_string, **options) super(Deserializer, self).__init__(stream_or_string, **options)
self.event_stream = pulldom.parse(self.stream, self._make_parser()) self.event_stream = pulldom.parse(self.stream, self._make_parser())
self.db = options.pop('using', DEFAULT_DB_ALIAS) self.db = options.pop('using', DEFAULT_DB_ALIAS)
self.ignore = options.pop('ignorenonexistent', False)
def _make_parser(self): def _make_parser(self):
"""Create a hardened XML parser (no custom/external entities).""" """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_accessor_attribute : [list_of_related_objects]})
m2m_data = {} m2m_data = {}
model_fields = Model._meta.get_all_field_names()
# Deseralize each field. # Deseralize each field.
for field_node in node.getElementsByTagName("field"): for field_node in node.getElementsByTagName("field"):
# If the field is missing the name attribute, bail (are you # 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 # Get the field from the Model. This will raise a
# FieldDoesNotExist if, well, the field doesn't exist, which will # 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) field = Model._meta.get_field(field_name)
# As is usually the case, relation fields get the special treatment. # As is usually the case, relation fields get the special treatment.

View File

@ -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 File

@ -87,6 +87,20 @@ class TestFixtures(TestCase):
) )
self.assertEqual(Animal.specimens.all()[0].name, 'Lion') 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') @skipIfDBFeature('interprets_empty_strings_as_nulls')
def test_pretty_print_xml(self): def test_pretty_print_xml(self):
""" """