Ticket #10301: serialzer_iterator.diff

File serialzer_iterator.diff, 2.6 KB (added by paulegan, 6 years ago)

Patch to add iterator support to Serializer

  • django/core/serializers/base.py

     
    3636
    3737        self.start_serialization()
    3838        for obj in queryset:
    39             self.start_object(obj)
    40             for field in obj._meta.local_fields:
    41                 if field.serialize:
    42                     if field.rel is None:
    43                         if self.selected_fields is None or field.attname in self.selected_fields:
    44                             self.handle_field(obj, field)
    45                     else:
    46                         if self.selected_fields is None or field.attname[:-3] in self.selected_fields:
    47                             self.handle_fk_field(obj, field)
    48             for field in obj._meta.many_to_many:
    49                 if field.serialize:
    50                     if self.selected_fields is None or field.attname in self.selected_fields:
    51                         self.handle_m2m_field(obj, field)
    52             self.end_object(obj)
     39            self.serialize_object(obj)
    5340        self.end_serialization()
    5441        return self.getvalue()
    5542
     43    def iterator(self, queryset, **options):
     44        """
     45        Iterate over serialized objects.
     46        """
     47        self.options = options
     48
     49        self.stream = StringIO()
     50        self.chunk_size = options.get('chunk_size', 1)
     51        self.selected_fields = options.get("fields")
     52
     53        self.start_serialization()
     54        for i, obj in enumerate(queryset):
     55            self.serialize_object(obj)
     56            if not i % self.chunk_size:
     57                yield self.stream.getvalue()
     58                self.stream.truncate(0)
     59        self.end_serialization()
     60        yield self.stream.getvalue()
     61
     62    def serialize_object(self, obj):
     63        self.start_object(obj)
     64        for field in obj._meta.local_fields:
     65            if field.serialize:
     66                if field.rel is None:
     67                    if self.selected_fields is None or field.attname in self.selected_fields:
     68                        self.handle_field(obj, field)
     69                else:
     70                    if self.selected_fields is None or field.attname[:-3] in self.selected_fields:
     71                        self.handle_fk_field(obj, field)
     72        for field in obj._meta.many_to_many:
     73            if field.serialize:
     74                if self.selected_fields is None or field.attname in self.selected_fields:
     75                    self.handle_m2m_field(obj, field)
     76        self.end_object(obj)
     77
    5678    def get_string_value(self, obj, field):
    5779        """
    5880        Convert a field's value to a string.
Back to Top