Skip to content

Commit

Permalink
Fixed a bug introduced in [6891]: the "safe" YAML serializer can't ha…
Browse files Browse the repository at this point in the history
…ndle time types out of the box. I've worked around the bug by coercing times to strings,

which is far from perfect. However, it's better than a Python-specific "!!python/time" type which would break interoperability (which was the goal of switching to the safe mode in the first place).


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6893 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jacobian committed Dec 4, 2007
1 parent b65fce6 commit 5684bdc
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions django/core/serializers/pyyaml.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
""" """


import datetime import datetime
from django.db import models
from django.core.serializers.python import Serializer as PythonSerializer from django.core.serializers.python import Serializer as PythonSerializer
from django.core.serializers.python import Deserializer as PythonDeserializer from django.core.serializers.python import Deserializer as PythonDeserializer
try: try:
Expand All @@ -17,6 +18,19 @@ class Serializer(PythonSerializer):
""" """
Convert a queryset to YAML. Convert a queryset to YAML.
""" """

def handle_field(self, 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 "!!python/time" type which would
# halt deserialization under any other language.
if isinstance(field, models.TimeField) and getattr(obj, field.name) is not None:
self._current[field.name] = str(getattr(obj, field.name))
else:
super(Serializer, self).handle_field(obj, field)

def end_serialization(self): def end_serialization(self):
self.options.pop('stream', None) self.options.pop('stream', None)
self.options.pop('fields', None) self.options.pop('fields', None)
Expand Down

0 comments on commit 5684bdc

Please sign in to comment.