Ticket #2843: include_reverse_relations.2.diff
File include_reverse_relations.2.diff, 3.8 KB (added by , 18 years ago) |
---|
-
django/core/serializers/xml_serializer.py
5 5 from django.conf import settings 6 6 from django.core.serializers import base 7 7 from django.db import models 8 from django.db.models.fields.related import OneToOneRel 8 9 from django.utils.xmlutils import SimplerXMLGenerator 9 10 from xml.dom import pulldom 10 11 … … 88 89 self.xml.addQuickElement("object", attrs={"pk" : str(relobj._get_pk_val())}) 89 90 self.xml.endElement("field") 90 91 92 def handle_o2m_field(self, obj, field): 93 if not isinstance(field.field.rel, OneToOneRel): 94 self.xml.startElement("field", { 95 "name" : field.get_accessor_name(), 96 "rel" : field.field.rel.__class__.__name__, 97 "to" : field.field.name, 98 }) 99 for relobj in getattr(obj, field.get_accessor_name()).iterator(): 100 self.xml.addQuickElement("object", attrs={"pk" : str(relobj._get_pk_val())}) 101 self.xml.endElement("field") 102 91 103 def _start_relational_field(self, field): 92 104 """ 93 105 Helper to output the <field> element for relational fields … … 218 230 inner_text.extend(getInnerText(child)) 219 231 else: 220 232 pass 221 return "".join(inner_text) 222 No newline at end of file 233 return "".join(inner_text) -
django/core/serializers/base.py
30 30 self.stream = options.get("stream", StringIO()) 31 31 self.selected_fields = options.get("fields") 32 32 33 self.include_reverse_relations = options.get("include_reverse_relations", False) 34 if self.options.has_key("include_reverse_relations"): 35 del self.options["include_reverse_relations"] 36 33 37 self.start_serialization() 34 38 for obj in queryset: 35 39 self.start_object(obj) … … 45 49 for field in obj._meta.many_to_many: 46 50 if self.selected_fields is None or field.attname in self.selected_fields: 47 51 self.handle_m2m_field(obj, field) 52 if self.include_reverse_relations: 53 for field in obj._meta.get_all_related_objects(): 54 self.handle_o2m_field(obj, field) 48 55 self.end_object(obj) 49 56 self.end_serialization() 50 57 return self.getvalue() … … 107 114 """ 108 115 raise NotImplementedError 109 116 117 def handle_o2m_field(self, obj, field): 118 """ 119 Called to handle a OneToManyField. 120 """ 121 raise NotImplementedError 122 110 123 def getvalue(self): 111 124 """ 112 125 Return the fully serialized queryset. -
django/core/serializers/python.py
7 7 from django.conf import settings 8 8 from django.core.serializers import base 9 9 from django.db import models 10 from django.db.models.fields.related import OneToOneRel 10 11 11 12 class Serializer(base.Serializer): 12 13 """ … … 43 44 def handle_m2m_field(self, obj, field): 44 45 self._current[field.name] = [related._get_pk_val() for related in getattr(obj, field.name).iterator()] 45 46 47 def handle_o2m_field(self, obj, field): 48 if not isinstance(field.field.rel, OneToOneRel): 49 self._current[field.get_accessor_name()] = [related._get_pk_val() for related in getattr(obj, field.get_accessor_name()).iterator()] 50 46 51 def getvalue(self): 47 52 return self.objects 48 53