Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added various fixes to serializer implementations. Fixes mostly deal …

…with handling nulls, non-integer primary key values (e.g., OneToOne fields or strings), and reconstruction of primary key references in related fields.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4718 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 375c3edf6edc0bb9f3e726f67c70fa524556713b 1 parent 400ee02
Russell Keith-Magee authored
6  django/core/serializers/base.py
@@ -54,11 +54,7 @@ def get_string_value(self, obj, field):
54 54
         Convert a field's value to a string.
55 55
         """
56 56
         if isinstance(field, models.DateTimeField):
57  
-            value = getattr(obj, field.name)
58  
-            if value is None:
59  
-                value = ''
60  
-            else:
61  
-                value = value.strftime("%Y-%m-%d %H:%M:%S")
  57
+            value = getattr(obj, field.name).strftime("%Y-%m-%d %H:%M:%S")
62 58
         elif isinstance(field, models.FileField):
63 59
             value = getattr(obj, "get_%s_url" % field.name, lambda: None)()
64 60
         else:
9  django/core/serializers/python.py
@@ -57,7 +57,7 @@ def Deserializer(object_list, **options):
57 57
     for d in object_list:
58 58
         # Look up the model and starting build a dict of data for it.
59 59
         Model = _get_model(d["model"])
60  
-        data = {Model._meta.pk.attname : d["pk"]}
  60
+        data = {Model._meta.pk.attname : Model._meta.pk.to_python(d["pk"])}
61 61
         m2m_data = {}
62 62
         
63 63
         # Handle each field
@@ -70,16 +70,17 @@ def Deserializer(object_list, **options):
70 70
             # Handle M2M relations
71 71
             if field.rel and isinstance(field.rel, models.ManyToManyRel):
72 72
                 pks = []
  73
+                m2m_convert = field.rel.to._meta.pk.to_python
73 74
                 for pk in field_value:
74 75
                     if isinstance(pk, unicode):
75  
-                        pks.append(pk.encode(options.get("encoding", settings.DEFAULT_CHARSET)))
  76
+                        pks.append(m2m_convert(pk.encode(options.get("encoding", settings.DEFAULT_CHARSET))))
76 77
                     else:
77  
-                        pks.append(pk)
  78
+                        pks.append(m2m_convert(pk))
78 79
                 m2m_data[field.name] = pks
79 80
                 
80 81
             # Handle FK fields
81 82
             elif field.rel and isinstance(field.rel, models.ManyToOneRel):
82  
-                data[field.attname] = field_value
  83
+                data[field.attname] = field.rel.to._meta.pk.to_python(field_value)
83 84
                     
84 85
             # Handle all other fields
85 86
             else:
31  django/core/serializers/xml_serializer.py
@@ -57,10 +57,12 @@ def handle_field(self, obj, field):
57 57
         })
58 58
         
59 59
         # Get a "string version" of the object's data (this is handled by the
60  
-        # serializer base class).  None is handled specially.
61  
-        value = self.get_string_value(obj, field)
62  
-        if value is not None:
  60
+        # serializer base class). 
  61
+        if getattr(obj, field.name) is not None:
  62
+            value = self.get_string_value(obj, field)
63 63
             self.xml.characters(str(value))
  64
+        else:
  65
+            self.xml.addQuickElement("None")
64 66
 
65 67
         self.xml.endElement("field")
66 68
         
@@ -127,7 +129,8 @@ def _handle_object(self, node):
127 129
         pk = node.getAttribute("pk")
128 130
         if not pk:
129 131
             raise base.DeserializationError("<object> node is missing the 'pk' attribute")
130  
-        data = {Model._meta.pk.name : pk}
  132
+
  133
+        data = {Model._meta.pk.attname : Model._meta.pk.to_python(pk)}
131 134
         
132 135
         # Also start building a dict of m2m data (this is saved as
133 136
         # {m2m_accessor_attribute : [list_of_related_objects]})
@@ -148,17 +151,20 @@ def _handle_object(self, node):
148 151
             
149 152
             # As is usually the case, relation fields get the special treatment.
150 153
             if field.rel and isinstance(field.rel, models.ManyToManyRel):
151  
-                m2m_data[field.name] = self._handle_m2m_field_node(field_node)
  154
+                m2m_data[field.name] = self._handle_m2m_field_node(field_node, field)
152 155
             elif field.rel and isinstance(field.rel, models.ManyToOneRel):
153  
-                data[field.attname] = self._handle_fk_field_node(field_node)
  156
+                data[field.attname] = self._handle_fk_field_node(field_node, field)
154 157
             else:
155  
-                value = field.to_python(getInnerText(field_node).strip().encode(self.encoding))
  158
+                if len(field_node.childNodes) == 1 and field_node.childNodes[0].nodeName == 'None':
  159
+                    value = None
  160
+                else:
  161
+                    value = field.to_python(getInnerText(field_node).strip().encode(self.encoding))
156 162
                 data[field.name] = value
157 163
         
158 164
         # Return a DeserializedObject so that the m2m data has a place to live.
159 165
         return base.DeserializedObject(Model(**data), m2m_data)
160 166
         
161  
-    def _handle_fk_field_node(self, node):
  167
+    def _handle_fk_field_node(self, node, field):
162 168
         """
163 169
         Handle a <field> node for a ForeignKey
164 170
         """
@@ -166,13 +172,16 @@ def _handle_fk_field_node(self, node):
166 172
         if len(node.childNodes) == 1 and node.childNodes[0].nodeName == 'None':
167 173
             return None
168 174
         else:
169  
-            return getInnerText(node).strip().encode(self.encoding)
  175
+            return field.rel.to._meta.pk.to_python(
  176
+                       getInnerText(node).strip().encode(self.encoding))
170 177
         
171  
-    def _handle_m2m_field_node(self, node):
  178
+    def _handle_m2m_field_node(self, node, field):
172 179
         """
173 180
         Handle a <field> node for a ManyToManyField
174 181
         """
175  
-        return [c.getAttribute("pk").encode(self.encoding) for c in node.getElementsByTagName("object")]
  182
+        return [field.rel.to._meta.pk.to_python(
  183
+                    c.getAttribute("pk").encode(self.encoding)) 
  184
+                    for c in node.getElementsByTagName("object")]
176 185
     
177 186
     def _get_model_from_node(self, node, attr):
178 187
         """

0 notes on commit 375c3ed

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