Ticket #2843: include_reverse_relations.3.diff
File include_reverse_relations.3.diff, 3.7 KB (added by , 13 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, DEFAULT_DB_ALIAS 8 from django.db.models.fields.related import OneToOneRel 8 9 from django.utils.xmlutils import SimplerXMLGenerator 9 10 from django.utils.encoding import smart_unicode 10 11 from xml.dom import pulldom … … 131 132 132 133 self.xml.endElement("field") 133 134 135 def handle_o2m_field(self, obj, field): 136 if not isinstance(field.field.rel, OneToOneRel): 137 self.xml.startElement("field", { 138 "name" : field.get_accessor_name(), 139 "rel" : field.field.rel.__class__.__name__, 140 "to" : field.field.name, 141 }) 142 for relobj in getattr(obj, field.get_accessor_name()).iterator(): 143 self.xml.addQuickElement("object", attrs={"pk" : str(relobj._get_pk_val())}) 144 self.xml.endElement("field") 145 134 146 def _start_relational_field(self, field): 135 147 """ 136 148 Helper to output the <field> element for relational fields -
django/core/serializers/base.py
38 38 self.selected_fields = options.pop("fields", None) 39 39 self.use_natural_keys = options.pop("use_natural_keys", False) 40 40 41 self.include_reverse_relations = options.get("include_reverse_relations", False) 42 if self.options.has_key("include_reverse_relations"): 43 del self.options["include_reverse_relations"] 44 41 45 self.start_serialization() 42 46 for obj in queryset: 43 47 self.start_object(obj) … … 56 60 if field.serialize: 57 61 if self.selected_fields is None or field.attname in self.selected_fields: 58 62 self.handle_m2m_field(obj, field) 63 if self.include_reverse_relations: 64 for field in concrete_model._meta.get_all_related_objects(): 65 self.handle_o2m_field(obj, field) 59 66 self.end_object(obj) 60 67 self.end_serialization() 61 68 return self.getvalue() … … 102 109 """ 103 110 raise NotImplementedError 104 111 112 def handle_o2m_field(self, obj, field): 113 """ 114 Called to handle a OneToManyField. 115 """ 116 raise NotImplementedError 117 105 118 def getvalue(self): 106 119 """ 107 120 Return the fully serialized queryset (or None if the output stream is -
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, DEFAULT_DB_ALIAS 10 from django.db.models.fields.related import OneToOneRel 10 11 from django.utils.encoding import smart_unicode, is_protected_type 11 12 12 13 class Serializer(base.Serializer): … … 64 65 self._current[field.name] = [m2m_value(related) 65 66 for related in getattr(obj, field.name).iterator()] 66 67 68 def handle_o2m_field(self, obj, field): 69 if not isinstance(field.field.rel, OneToOneRel): 70 self._current[field.get_accessor_name()] = [related._get_pk_val() for related in getattr(obj, field.get_accessor_name()).iterator()] 71 67 72 def getvalue(self): 68 73 return self.objects 69 74