MCPcopy
hub / github.com/django/django / _handle_object

Method _handle_object

django/core/serializers/xml_serializer.py:254–338  ·  view source on GitHub ↗

Convert an node to a DeserializedObject.

(self, node)

Source from the content-addressed store, hash-verified

252 raise StopIteration
253
254 def _handle_object(self, node):
255 """Convert an <object> node to a DeserializedObject."""
256 # Look up the model using the model loading mechanism. If this fails,
257 # bail.
258 Model = self._get_model_from_node(node, "model")
259
260 # Start building a data dictionary from the object.
261 data = {}
262 if node.hasAttribute("pk"):
263 data[Model._meta.pk.attname] = Model._meta.pk.to_python(
264 node.getAttribute("pk")
265 )
266
267 # Also start building a dict of m2m data (this is saved as
268 # {m2m_accessor_attribute : [list_of_related_objects]})
269 m2m_data = {}
270 deferred_fields = {}
271
272 field_names = {f.name for f in Model._meta.get_fields()}
273 # Deserialize each field.
274 for field_node in getChildrenByTagName(node, "field"):
275 # If the field is missing the name attribute, bail (are you
276 # sensing a pattern here?)
277 field_name = field_node.getAttribute("name")
278 if not field_name:
279 raise base.DeserializationError(
280 "<field> node is missing the 'name' attribute"
281 )
282
283 # Get the field from the Model. This will raise a
284 # FieldDoesNotExist if, well, the field doesn't exist, which will
285 # be propagated correctly unless ignorenonexistent=True is used.
286 if self.ignore and field_name not in field_names:
287 continue
288 field = Model._meta.get_field(field_name)
289
290 # As is usually the case, relation fields get the special
291 # treatment.
292 if field.remote_field and isinstance(
293 field.remote_field, models.ManyToManyRel
294 ):
295 value = self._handle_m2m_field_node(field_node, field)
296 if value == base.DEFER_FIELD:
297 deferred_fields[field] = [
298 [
299 (
300 None
301 if getChildrenByTagName(nat_node, "None")
302 else getInnerText(nat_node).strip()
303 )
304 for nat_node in getChildrenByTagName(obj_node, "natural")
305 ]
306 for obj_node in getChildrenByTagName(field_node, "object")
307 ]
308 else:
309 m2m_data[field.name] = value
310 elif field.remote_field and isinstance(
311 field.remote_field, models.ManyToOneRel

Callers 1

__next__Method · 0.95

Calls 10

_get_model_from_nodeMethod · 0.95
_handle_fk_field_nodeMethod · 0.95
getChildrenByTagNameFunction · 0.85
getInnerTextFunction · 0.85
to_pythonMethod · 0.45
get_fieldsMethod · 0.45
get_fieldMethod · 0.45
get_internal_typeMethod · 0.45
loadsMethod · 0.45

Tested by

no test coverage detected