yaml serializer requires pyyaml https pyyaml org but that s checked for in init use the c faster implementation if possible workaround to represent dictionaries in insertion order see https github comyamlpyyamlpull143 convert a queryset to yaml internaluseonly false def handlefieldself obj field a nasty special case base yaml doesn t support serialization of time types as opposed to dates or datetimes which it does support since we want to use the safe serializer for better interoperability we need to do something with those pesky times converting em to strings isn t perfect but it s better than a pythontime type which would halt deserialization under any other language if isinstancefield models timefield and getattrobj field name is not none self currentfield name strgetattrobj field name else super handlefieldobj field def endserializationself self options setdefaultallowunicode true yaml dumpself objects self stream dumperdjangosafedumper self options def getvalueself grandparent super return superpythonserializer self getvalue def deserializerstreamorstring options use the c faster implementation if possible workaround to represent dictionaries in insertion order see https github com yaml pyyaml pull 143 convert a queryset to yaml a nasty special case base yaml doesn t support serialization of time types as opposed to dates or datetimes which it does support since we want to use the safe serializer for better interoperability we need to do something with those pesky times converting em to strings isn t perfect but it s better than a python time type which would halt deserialization under any other language grandparent super deserialize a stream or string of yaml data
import collections
import decimal
from io import StringIO

import yaml

from django.core.serializers.base import DeserializationError
from django.core.serializers.python import Deserializer as PythonDeserializer
from django.core.serializers.python import Serializer as PythonSerializer
from django.db import models


try:
    from yaml import CSafeDumper as SafeDumper
    from yaml import CSafeLoader as SafeLoader
except ImportError:
    from yaml import SafeDumper, SafeLoader


class DjangoSafeDumper(SafeDumper):
    def represent_decimal(self, data):
        return self.represent_scalar("tag:yaml.org,2002:str", str(data))

    def represent_ordered_dict(self, data):
        return self.represent_mapping("tag:yaml.org,2002:map", data.items())


DjangoSafeDumper.add_representer(decimal.Decimal, DjangoSafeDumper.represent_decimal)
DjangoSafeDumper.add_representer(
    collections.OrderedDict, DjangoSafeDumper.represent_ordered_dict
)


DjangoSafeDumper.add_representer(dict, DjangoSafeDumper.represent_ordered_dict)


class Serializer(PythonSerializer):
    

    internal_use_only = False

    def handle_field(self, obj, field):
        
        
        
        
        
        
        if isinstance(field, models.TimeField) and getattr(obj, field.name) is not None:
            self._current[field.name] = str(getattr(obj, field.name))
        else:
            super().handle_field(obj, field)

    def end_serialization(self):
        self.options.setdefault("allow_unicode", True)
        yaml.dump(self.objects, self.stream, Dumper=DjangoSafeDumper, **self.options)

    def getvalue(self):
        
        return super(PythonSerializer, self).getvalue()


def Deserializer(stream_or_string, **options):
    
    if isinstance(stream_or_string, bytes):
        stream_or_string = stream_or_string.decode()
    if isinstance(stream_or_string, str):
        stream = StringIO(stream_or_string)
    else:
        stream = stream_or_string
    try:
        yield from PythonDeserializer(yaml.load(stream, Loader=SafeLoader), **options)
    except (GeneratorExit, DeserializationError):
        raise
    except Exception as exc:
        raise DeserializationError() from exc