Ticket #2843: svn.diff

File svn.diff, 3.4 KB (added by "Nikolaus Schlemm" <nikl@…>, 9 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

  • 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
     
    214226            inner_text.extend(getInnerText(child))
    215227        else:
    216228           pass
    217     return "".join(inner_text)
    218  No newline at end of file
     229    return "".join(inner_text)
  • base.py

     
    2929
    3030        self.stream = options.get("stream", StringIO())
    3131
     32        self.export_o2m = options.get("export_o2m", False)
     33        if self.options.has_key("export_o2m"):
     34            del self.options["export_o2m"]
     35
    3236        self.start_serialization()
    3337        for obj in queryset:
    3438            self.start_object(obj)
     
    4145                    self.handle_fk_field(obj, field)
    4246            for field in obj._meta.many_to_many:
    4347                self.handle_m2m_field(obj, field)
     48            if self.export_o2m:
     49                for field in obj._meta.get_all_related_objects():
     50                    self.handle_o2m_field(obj, field)
    4451            self.end_object(obj)
    4552        self.end_serialization()
    4653        return self.getvalue()
     
    103110        """
    104111        raise NotImplementedError
    105112
     113    def handle_o2m_field(self, obj, field):
     114        """
     115        Called to handle a OneToManyField.
     116        """
     117        raise NotImplementedError
     118
    106119    def getvalue(self):
    107120        """
    108121        Return the fully serialized queryset.
  • 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