Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #2611 -- Fixed XML serializer to handle null datetime fields. T…

…hanks for reporting, csdurfee@gmail.com

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3687 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 22303d6c7dfbcaa90ea2f6d2792c43b9b510e1f0 1 parent 452847a
Adrian Holovaty authored August 31, 2006

Showing 1 changed file with 29 additions and 25 deletions. Show diff stats Hide diff stats

  1. 54  django/core/serializers/base.py
54  django/core/serializers/base.py
@@ -11,7 +11,7 @@
11 11
 class SerializationError(Exception):
12 12
     """Something bad happened during serialization."""
13 13
     pass
14  
-    
  14
+
15 15
 class DeserializationError(Exception):
16 16
     """Something bad happened during deserialization."""
17 17
     pass
@@ -20,15 +20,15 @@ class Serializer(object):
20 20
     """
21 21
     Abstract serializer base class.
22 22
     """
23  
-    
  23
+
24 24
     def serialize(self, queryset, **options):
25 25
         """
26 26
         Serialize a queryset.
27 27
         """
28 28
         self.options = options
29  
-        
  29
+
30 30
         self.stream = options.get("stream", StringIO())
31  
-        
  31
+
32 32
         self.start_serialization()
33 33
         for obj in queryset:
34 34
             self.start_object(obj)
@@ -44,61 +44,65 @@ def serialize(self, queryset, **options):
44 44
             self.end_object(obj)
45 45
         self.end_serialization()
46 46
         return self.getvalue()
47  
-    
  47
+
48 48
     def get_string_value(self, obj, field):
49 49
         """
50 50
         Convert a field's value to a string.
51 51
         """
52 52
         if isinstance(field, models.DateTimeField):
53  
-            value = getattr(obj, field.name).strftime("%Y-%m-%d %H:%M:%S")
  53
+            value = getattr(obj, field.name)
  54
+            if value is None:
  55
+                value = ''
  56
+            else:
  57
+                value = value.strftime("%Y-%m-%d %H:%M:%S")
54 58
         elif isinstance(field, models.FileField):
55 59
             value = getattr(obj, "get_%s_url" % field.name, lambda: None)()
56 60
         else:
57 61
             value = field.flatten_data(follow=None, obj=obj).get(field.name, "")
58 62
         return str(value)
59  
-    
  63
+
60 64
     def start_serialization(self):
61 65
         """
62 66
         Called when serializing of the queryset starts.
63 67
         """
64 68
         raise NotImplementedError
65  
-    
  69
+
66 70
     def end_serialization(self):
67 71
         """
68 72
         Called when serializing of the queryset ends.
69 73
         """
70 74
         pass
71  
-    
  75
+
72 76
     def start_object(self, obj):
73 77
         """
74 78
         Called when serializing of an object starts.
75 79
         """
76 80
         raise NotImplementedError
77  
-    
  81
+
78 82
     def end_object(self, obj):
79 83
         """
80 84
         Called when serializing of an object ends.
81 85
         """
82 86
         pass
83  
-    
  87
+
84 88
     def handle_field(self, obj, field):
85 89
         """
86 90
         Called to handle each individual (non-relational) field on an object.
87 91
         """
88 92
         raise NotImplementedError
89  
-    
  93
+
90 94
     def handle_fk_field(self, obj, field):
91 95
         """
92 96
         Called to handle a ForeignKey field.
93 97
         """
94 98
         raise NotImplementedError
95  
-    
  99
+
96 100
     def handle_m2m_field(self, obj, field):
97 101
         """
98 102
         Called to handle a ManyToManyField.
99 103
         """
100 104
         raise NotImplementedError
101  
-    
  105
+
102 106
     def getvalue(self):
103 107
         """
104 108
         Return the fully serialized queryset.
@@ -109,7 +113,7 @@ class Deserializer(object):
109 113
     """
110 114
     Abstract base deserializer class.
111 115
     """
112  
-    
  116
+
113 117
     def __init__(self, stream_or_string, **options):
114 118
         """
115 119
         Init this serializer given a stream or a string
@@ -123,39 +127,39 @@ def __init__(self, stream_or_string, **options):
123 127
         # deserialization starts (otherwise subclass calls to get_model()
124 128
         # and friends might fail...)
125 129
         models.get_apps()
126  
-    
  130
+
127 131
     def __iter__(self):
128 132
         return self
129  
-    
  133
+
130 134
     def next(self):
131 135
         """Iteration iterface -- return the next item in the stream"""
132 136
         raise NotImplementedError
133  
-        
  137
+
134 138
 class DeserializedObject(object):
135 139
     """
136 140
     A deserialzed model.
137  
-    
  141
+
138 142
     Basically a container for holding the pre-saved deserialized data along
139 143
     with the many-to-many data saved with the object.
140  
-    
  144
+
141 145
     Call ``save()`` to save the object (with the many-to-many data) to the
142 146
     database; call ``save(save_m2m=False)`` to save just the object fields
143 147
     (and not touch the many-to-many stuff.)
144 148
     """
145  
-    
  149
+
146 150
     def __init__(self, obj, m2m_data=None):
147 151
         self.object = obj
148 152
         self.m2m_data = m2m_data
149  
-        
  153
+
150 154
     def __repr__(self):
151 155
         return "<DeserializedObject: %s>" % str(self.object)
152  
-        
  156
+
153 157
     def save(self, save_m2m=True):
154 158
         self.object.save()
155 159
         if self.m2m_data and save_m2m:
156 160
             for accessor_name, object_list in self.m2m_data.items():
157 161
                 setattr(self.object, accessor_name, object_list)
158  
-        
159  
-        # prevent a second (possibly accidental) call to save() from saving 
  162
+
  163
+        # prevent a second (possibly accidental) call to save() from saving
160 164
         # the m2m data twice.
161 165
         self.m2m_data = None

0 notes on commit 22303d6

Please sign in to comment.
Something went wrong with that request. Please try again.