def restore_object(self, attrs, instance=None): """ Restore the model instance. """ m2m_data = {} for field in self.opts.model._meta.many_to_many: if field.name in attrs: m2m_data[field.name] = attrs.pop(field.name) return DeserializedObject(self.opts.model(**attrs), m2m_data)
def Deserializer(json, using=DEFAULT_DB_ALIAS, **options): # TODO: fix """ Deserialize JSON back into Django ORM instances. Django deserializers yield a DeserializedObject generator. DeserializedObjects are thin wrappers over POPOs. """ m2m_data = {} # Generate the serializer ModelDeserializer = ConceptSerializerFactory().generate_deserializer(json) # Instantiate the serializer data = JSON.loads(json) Model = apps.get_model(data['serialized_model']) # Deserialize the data serializer = ModelDeserializer(data=data) serializer.is_valid(raise_exception=True) obj = build_instance(Model, data, using) yield DeserializedObject(obj, m2m_data)
def test_repr(self): author = Author(name='John', pk=1) deserial_obj = DeserializedObject(obj=author) self.assertEqual(repr(deserial_obj), '<DeserializedObject: serializers.Author(pk=1)>')
def handle_object(self, node): """ Convert a ``<file>``-node to a ``DeserializedObject``. :param node: The current xml node of the object :type node: xml.dom.minidom.Element :raises ~django.core.serializers.base.DeserializationError: If the deserialization fails :raises ~django.core.exceptions.FieldDoesNotExist: If the XLIFF file contains a field which doesn't exist on the :return: The deserialized page translation :rtype: django.core.serializers.base.DeserializedObject PageTranslation model """ # Get page translation (process varies for the different xliff versions) page_translation = self.get_object(node) logger.debug( "Existing page translation: %r", page_translation, ) # Increment the version number page_translation.version = page_translation.version + 1 # Make sure object is not in translation anymore if it was before page_translation.currently_in_translation = False # Set the id to None to make sure a new object is stored in the database when save() is called page_translation.id = None # Deserialize each field. for field_node in node.getElementsByTagName(self.unit_node): # Check to which attribute this resource belongs to field_name = self.require_attribute(field_node, "resname") # Get the field from the PageTranslation model try: field = page_translation._meta.get_field(field_name) except FieldDoesNotExist as e: # If the field doesn't exist, check if a legacy field is supported field_name = settings.XLIFF_LEGACY_FIELDS.get(field_name) try: field = page_translation._meta.get_field(field_name) except FieldDoesNotExist: # If the legacy field doesn't exist as well, just raise the initial exception # pylint: disable=raise-missing-from raise e # Now get the actual target value of the field target = field_node.getElementsByTagName("target") if not target: raise DeserializationError( f"Field {field_name} does not contain a <target> node." ) # Set the field attribute of the page translation to the new target value setattr( page_translation, field_name, field.to_python(xml_serializer.getInnerText(target[0]).strip()), ) logger.debug("Deserialized page translation: %r", page_translation) # Return a DeserializedObject return DeserializedObject(page_translation)