Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…res.
  • Loading branch information...
commit 465b01f065a8482bfa42c19e3854a3c810e69d33 1 parent 6c730da
Christoph Sieghart authored March 06, 2013 apollo13 committed March 30, 2013
6  django/core/serializers/xml_serializer.py
@@ -155,6 +155,7 @@ def __init__(self, stream_or_string, **options):
155 155
         super(Deserializer, self).__init__(stream_or_string, **options)
156 156
         self.event_stream = pulldom.parse(self.stream, self._make_parser())
157 157
         self.db = options.pop('using', DEFAULT_DB_ALIAS)
  158
+        self.ignore = options.pop('ignorenonexistent', False)
158 159
 
159 160
     def _make_parser(self):
160 161
         """Create a hardened XML parser (no custom/external entities)."""
@@ -188,6 +189,7 @@ def _handle_object(self, node):
188 189
         # {m2m_accessor_attribute : [list_of_related_objects]})
189 190
         m2m_data = {}
190 191
 
  192
+        model_fields = Model._meta.get_all_field_names()
191 193
         # Deseralize each field.
192 194
         for field_node in node.getElementsByTagName("field"):
193 195
             # If the field is missing the name attribute, bail (are you
@@ -198,7 +200,9 @@ def _handle_object(self, node):
198 200
 
199 201
             # Get the field from the Model. This will raise a
200 202
             # FieldDoesNotExist if, well, the field doesn't exist, which will
201  
-            # be propagated correctly.
  203
+            # be propagated correctly unless ignorenonexistent=True is used.
  204
+            if self.ignore and field_name not in model_fields:
  205
+                continue
202 206
             field = Model._meta.get_field(field_name)
203 207
 
204 208
             # As is usually the case, relation fields get the special treatment.
10  tests/fixtures_regress/fixtures/sequence_extra_xml.xml
... ...
@@ -0,0 +1,10 @@
  1
+<?xml version="1.0" encoding="utf-8"?>
  2
+<django-objects version="1.0">
  3
+    <object pk="1" model="fixtures_regress.animal">
  4
+        <field type="CharField" name="name">Wolf</field>
  5
+        <field type="CharField" name="extra_name">Super Wolf</field>
  6
+        <field type="CharField" name="latin_name">Canis lupus</field>
  7
+        <field type="IntegerField" name="count">3</field>
  8
+        <field type="FloatField" name="weight">1.2</field>
  9
+    </object>
  10
+</django-objects>
14  tests/fixtures_regress/tests.py
@@ -87,6 +87,20 @@ def test_loaddata_not_found_fields_ignore(self):
87 87
         )
88 88
         self.assertEqual(Animal.specimens.all()[0].name, 'Lion')
89 89
 
  90
+    def test_loaddata_not_found_fields_ignore_xml(self):
  91
+        """
  92
+        Test for ticket #19998 -- Ignore entries in the XML serialised data
  93
+        for fields that have been removed from the model definition.
  94
+        """
  95
+        management.call_command(
  96
+            'loaddata',
  97
+            'sequence_extra_xml',
  98
+            ignore=True,
  99
+            verbosity=0,
  100
+            commit=False
  101
+        )
  102
+        self.assertEqual(Animal.specimens.all()[0].name, 'Wolf')
  103
+
90 104
     @skipIfDBFeature('interprets_empty_strings_as_nulls')
91 105
     def test_pretty_print_xml(self):
92 106
         """

0 notes on commit 465b01f

Please sign in to comment.
Something went wrong with that request. Please try again.