Ticket #5423: django_dumpdata_streamed_output_3.diff

File django_dumpdata_streamed_output_3.diff, 6.5 KB (added by calvin@…, 17 years ago)

Newest version, fixing modeltests

  • django/core/serializers/json.py

     
    44
    55import datetime
    66from django.utils import simplejson
    7 from django.utils.simplejson import decoder
    87from django.core.serializers.python import Serializer as PythonSerializer
    98from django.core.serializers.python import Deserializer as PythonDeserializer
    109try:
     
    2221    """
    2322    internal_use_only = False
    2423   
     24    def start_serialization(self):
     25        self._current = None
     26        self.json_kwargs = self.options.copy()
     27        self.json_kwargs.pop('stream', None)
     28        self.json_kwargs.pop('fields', None)
     29        self.stream.write("[")
     30
    2531    def end_serialization(self):
    26         self.options.pop('stream', None)
    27         self.options.pop('fields', None)
    28         simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder, **self.options)
     32        if self.options.get("indent"):
     33            self.stream.write("\n")
     34        self.stream.write("]")
     35        if self.options.get("indent"):
     36            self.stream.write("\n")
    2937
     38    def end_object(self, obj):
     39        # self._current has the field data
     40        if not self.first:
     41            self.stream.write(",")
     42            if not self.options.get("indent"):
     43                self.stream.write(" ")
     44        if self.options.get("indent"):
     45            self.stream.write("\n")
     46        simplejson.dump(self.get_dump_object(obj), self.stream,
     47                        cls=DjangoJSONEncoder, **self.json_kwargs)
     48        self._current = None
     49
    3050    def getvalue(self):
     51        # overwrite PythonSerializer.getvalue() with base Serializer.getvalue()
    3152        if callable(getattr(self.stream, 'getvalue', None)):
    3253            return self.stream.getvalue()
    3354
  • django/core/serializers/base.py

     
    3636        self.selected_fields = options.get("fields")
    3737
    3838        self.start_serialization()
     39        self.first = True
    3940        for obj in queryset:
    4041            self.start_object(obj)
    4142            for field in obj._meta.fields:
     
    5152                    if self.selected_fields is None or field.attname in self.selected_fields:
    5253                        self.handle_m2m_field(obj, field)
    5354            self.end_object(obj)
     55            if self.first:
     56                self.first = False
    5457        self.end_serialization()
    5558        return self.getvalue()
    5659
     
    5962        Convert a field's value to a string.
    6063        """
    6164        if isinstance(field, models.DateTimeField):
    62             value = getattr(obj, field.name).strftime("%Y-%m-%d %H:%M:%S")
     65            value = getattr(obj, field.name)
     66            if value is None:
     67                value = ""
     68            else:
     69                value = value.strftime("%Y-%m-%d %H:%M:%S")
    6370        elif isinstance(field, models.FileField):
    6471            value = getattr(obj, "get_%s_url" % field.name, lambda: None)()
    6572        else:
  • django/core/serializers/python.py

     
    2727        self._current = {}
    2828
    2929    def end_object(self, obj):
    30         self.objects.append({
     30        self.objects.append(self.get_dump_object(obj))
     31        self._current = None
     32
     33    def get_dump_object (self, obj):
     34        return {
    3135            "model"  : smart_unicode(obj._meta),
    3236            "pk"     : smart_unicode(obj._get_pk_val(), strings_only=True),
    3337            "fields" : self._current
    34         })
    35         self._current = None
     38        }
    3639
    3740    def handle_field(self, obj, field):
    38         self._current[field.name] = smart_unicode(getattr(obj, field.name), strings_only=True)
     41        value = smart_unicode(getattr(obj, field.name), strings_only=True)
     42        self._current[field.name] = value
    3943
    4044    def handle_fk_field(self, obj, field):
    4145        related = getattr(obj, field.name)
  • django/core/management/commands/dumpdata.py

     
    22from django.core import serializers
    33
    44from optparse import make_option
     5import sys
    56
    67class Command(BaseCommand):
    78    option_list = BaseCommand.option_list + (
     
    3536        except KeyError:
    3637            raise CommandError("Unknown serialization format: %s" % format)
    3738
    38         objects = []
    39         for app in app_list:
    40             for model in get_models(app):
    41                 objects.extend(model._default_manager.all())
     39        def get_objects ():
     40            for app in app_list:
     41                for model in get_models(app):
     42                    for obj in model._default_manager.order_by(model._meta.pk.attname).iterator():
     43                        yield obj
    4244        try:
    43             return serializers.serialize(format, objects, indent=indent)
     45            serializers.serialize(format, get_objects(), indent=indent, stream=sys.stdout)
    4446        except Exception, e:
    4547            if show_traceback:
    4648                raise
  • tests/modeltests/fixtures/models.py

     
    8181
    8282# Dump the current contents of the database as a JSON fixture
    8383>>> management.call_command('dumpdata', 'fixtures', format='json')
    84 [{"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]
     84[{"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}]
    8585"""
    8686
    8787from django.test import TestCase
Back to Top