Ticket #2843: include_reverse_relations.2.diff

File include_reverse_relations.2.diff, 3.8 KB (added by Nikolaus Schlemm <nikl@…>, 9 years ago)

argh! forgot django/core/serializers/python.py in the last diff...

  • django/core/serializers/xml_serializer.py

     
    55from django.conf import settings
    66from django.core.serializers import base
    77from django.db import models
     8from django.db.models.fields.related import OneToOneRel
    89from django.utils.xmlutils import SimplerXMLGenerator
    910from xml.dom import pulldom
    1011
     
    8889            self.xml.addQuickElement("object", attrs={"pk" : str(relobj._get_pk_val())})
    8990        self.xml.endElement("field")
    9091       
     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   
    91103    def _start_relational_field(self, field):
    92104        """
    93105        Helper to output the <field> element for relational fields
     
    218230            inner_text.extend(getInnerText(child))
    219231        else:
    220232           pass
    221     return "".join(inner_text)
    222  No newline at end of file
     233    return "".join(inner_text)
  • django/core/serializers/base.py

     
    3030        self.stream = options.get("stream", StringIO())
    3131        self.selected_fields = options.get("fields")
    3232
     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
    3337        self.start_serialization()
    3438        for obj in queryset:
    3539            self.start_object(obj)
     
    4549            for field in obj._meta.many_to_many:
    4650                if self.selected_fields is None or field.attname in self.selected_fields:
    4751                    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)
    4855            self.end_object(obj)
    4956        self.end_serialization()
    5057        return self.getvalue()
     
    107114        """
    108115        raise NotImplementedError
    109116
     117    def handle_o2m_field(self, obj, field):
     118        """
     119        Called to handle a OneToManyField.
     120        """
     121        raise NotImplementedError
     122
    110123    def getvalue(self):
    111124        """
    112125        Return the fully serialized queryset.
  • django/core/serializers/python.py

     
    77from django.conf import settings
    88from django.core.serializers import base
    99from django.db import models
     10from django.db.models.fields.related import OneToOneRel
    1011
    1112class Serializer(base.Serializer):
    1213    """
     
    4344    def handle_m2m_field(self, obj, field):
    4445        self._current[field.name] = [related._get_pk_val() for related in getattr(obj, field.name).iterator()]
    4546   
     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   
    4651    def getvalue(self):
    4752        return self.objects
    4853
Back to Top