Django

Code

Changeset 6893

Show
Ignore:
Timestamp:
12/04/07 15:35:28 (1 year ago)
Author:
jacob
Message:

Fixed a bug introduced in [6891]: the "safe" YAML serializer can't handle 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).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/serializers/pyyaml.py

    r6891 r6893  
    66 
    77import datetime 
     8from django.db import models 
    89from django.core.serializers.python import Serializer as PythonSerializer 
    910from django.core.serializers.python import Deserializer as PythonDeserializer 
     
    1819    Convert a queryset to YAML. 
    1920    """ 
     21     
     22    def handle_field(self, obj, field): 
     23        # A nasty special case: base YAML doesn't support serialization of time 
     24        # types (as opposed to dates or datetimes, which it does support). Since 
     25        # we want to use the "safe" serializer for better interoperability, we 
     26        # need to do something with those pesky times. Converting 'em to strings 
     27        # isn't perfect, but it's better than a "!!python/time" type which would 
     28        # halt deserialization under any other language. 
     29        if isinstance(field, models.TimeField) and getattr(obj, field.name) is not None: 
     30            self._current[field.name] = str(getattr(obj, field.name)) 
     31        else: 
     32            super(Serializer, self).handle_field(obj, field) 
     33     
    2034    def end_serialization(self): 
    2135        self.options.pop('stream', None)