app/django/core/serializers/base.py
changeset 323 ff1a9aa48cfd
parent 54 03e267d67478
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
     1 """
     1 """
     2 Module for abstract serializer/unserializer base classes.
     2 Module for abstract serializer/unserializer base classes.
     3 """
     3 """
     4 
     4 
     5 try:
     5 from StringIO import StringIO
     6     from cStringIO import StringIO
     6 
     7 except ImportError:
       
     8     from StringIO import StringIO
       
     9 from django.db import models
     7 from django.db import models
    10 from django.utils.encoding import smart_str, smart_unicode
     8 from django.utils.encoding import smart_str, smart_unicode
       
     9 from django.utils import datetime_safe
    11 
    10 
    12 class SerializationError(Exception):
    11 class SerializationError(Exception):
    13     """Something bad happened during serialization."""
    12     """Something bad happened during serialization."""
    14     pass
    13     pass
    15 
    14 
    36         self.selected_fields = options.get("fields")
    35         self.selected_fields = options.get("fields")
    37 
    36 
    38         self.start_serialization()
    37         self.start_serialization()
    39         for obj in queryset:
    38         for obj in queryset:
    40             self.start_object(obj)
    39             self.start_object(obj)
    41             for field in obj._meta.fields:
    40             for field in obj._meta.local_fields:
    42                 if field.serialize:
    41                 if field.serialize:
    43                     if field.rel is None:
    42                     if field.rel is None:
    44                         if self.selected_fields is None or field.attname in self.selected_fields:
    43                         if self.selected_fields is None or field.attname in self.selected_fields:
    45                             self.handle_field(obj, field)
    44                             self.handle_field(obj, field)
    46                     else:
    45                     else:
    56 
    55 
    57     def get_string_value(self, obj, field):
    56     def get_string_value(self, obj, field):
    58         """
    57         """
    59         Convert a field's value to a string.
    58         Convert a field's value to a string.
    60         """
    59         """
    61         if isinstance(field, models.DateTimeField):
    60         return smart_unicode(field.value_to_string(obj))
    62             value = getattr(obj, field.name).strftime("%Y-%m-%d %H:%M:%S")
       
    63         else:
       
    64             value = field.flatten_data(follow=None, obj=obj).get(field.name, "")
       
    65         return smart_unicode(value)
       
    66 
    61 
    67     def start_serialization(self):
    62     def start_serialization(self):
    68         """
    63         """
    69         Called when serializing of the queryset starts.
    64         Called when serializing of the queryset starts.
    70         """
    65         """