Code

Ticket #2843: svn.diff

File svn.diff, 3.4 KB (added by "Nikolaus Schlemm" <nikl@…>, 8 years ago)

no time on the weekend, but now it's done :)

one small change to what I previously wrote: using "article_set" instead of "articles" seemed much more consistent with the rest of the django api

Line 
1Index: xml_serializer.py
2===================================================================
3--- xml_serializer.py   (revision 3896)
4+++ xml_serializer.py   (working copy)
5@@ -5,6 +5,7 @@
6 from django.conf import settings
7 from django.core.serializers import base
8 from django.db import models
9+from django.db.models.fields.related import OneToOneRel
10 from django.utils.xmlutils import SimplerXMLGenerator
11 from xml.dom import pulldom
12 
13@@ -88,6 +89,17 @@
14             self.xml.addQuickElement("object", attrs={"pk" : str(relobj._get_pk_val())})
15         self.xml.endElement("field")
16         
17+    def handle_o2m_field(self, obj, field):
18+        if not isinstance(field.field.rel, OneToOneRel):
19+            self.xml.startElement("field", {
20+                "name" : field.get_accessor_name(),
21+                "rel"  : field.field.rel.__class__.__name__,
22+                "to"   : field.field.name,
23+            })
24+            for relobj in getattr(obj, field.get_accessor_name()).iterator():
25+                self.xml.addQuickElement("object", attrs={"pk" : str(relobj._get_pk_val())})
26+            self.xml.endElement("field")
27+   
28     def _start_relational_field(self, field):
29         """
30         Helper to output the <field> element for relational fields
31@@ -214,4 +226,4 @@
32             inner_text.extend(getInnerText(child))
33         else:
34            pass
35-    return "".join(inner_text)
36\ No newline at end of file
37+    return "".join(inner_text)
38Index: base.py
39===================================================================
40--- base.py     (revision 3896)
41+++ base.py     (working copy)
42@@ -29,6 +29,10 @@
43 
44         self.stream = options.get("stream", StringIO())
45 
46+        self.export_o2m = options.get("export_o2m", False)
47+        if self.options.has_key("export_o2m"):
48+            del self.options["export_o2m"]
49+
50         self.start_serialization()
51         for obj in queryset:
52             self.start_object(obj)
53@@ -41,6 +45,9 @@
54                     self.handle_fk_field(obj, field)
55             for field in obj._meta.many_to_many:
56                 self.handle_m2m_field(obj, field)
57+            if self.export_o2m:
58+                for field in obj._meta.get_all_related_objects():
59+                    self.handle_o2m_field(obj, field)
60             self.end_object(obj)
61         self.end_serialization()
62         return self.getvalue()
63@@ -103,6 +110,12 @@
64         """
65         raise NotImplementedError
66 
67+    def handle_o2m_field(self, obj, field):
68+        """
69+        Called to handle a OneToManyField.
70+        """
71+        raise NotImplementedError
72+
73     def getvalue(self):
74         """
75         Return the fully serialized queryset.
76Index: python.py
77===================================================================
78--- python.py   (revision 3896)
79+++ python.py   (working copy)
80@@ -7,6 +7,7 @@
81 from django.conf import settings
82 from django.core.serializers import base
83 from django.db import models
84+from django.db.models.fields.related import OneToOneRel
85 
86 class Serializer(base.Serializer):
87     """
88@@ -43,6 +44,10 @@
89     def handle_m2m_field(self, obj, field):
90         self._current[field.name] = [related._get_pk_val() for related in getattr(obj, field.name).iterator()]
91     
92+    def handle_o2m_field(self, obj, field):
93+        if not isinstance(field.field.rel, OneToOneRel):
94+            self._current[field.get_accessor_name()] = [related._get_pk_val() for related in getattr(obj, field.get_accessor_name()).iterator()]
95+   
96     def getvalue(self):
97         return self.objects
98