Ticket #5423: django_dumpdata_streamed_output.diff

File django_dumpdata_streamed_output.diff, 5.4 KB (added by Bastian Kleineidam <calvin@…>, 17 years ago)

streamed json output

  • django/core/management/commands/dumpdata.py

     
    11from django.core.management.base import BaseCommand, CommandError
    2 
    32from optparse import make_option
     3import sys
    44
     5
    56class Command(BaseCommand):
    67    option_list = BaseCommand.option_list + (
    78        make_option('--format', default='json', dest='format',
     
    3132        except KeyError:
    3233            raise CommandError("Unknown serialization format: %s" % format)
    3334
    34         objects = []
    35         for app in app_list:
    36             for model in get_models(app):
    37                 objects.extend(model.objects.all())
     35        def get_objects ():
     36            for app in app_list:
     37                for model in get_models(app):
     38                    for obj in model.objects.all().order_by(model._meta.pk.attname):
     39                        yield obj
    3840        try:
    39             return serializers.serialize(format, objects, indent=indent)
     41            serializers.serialize(format, get_objects(), indent=indent, stream=sys.stdout)
    4042        except Exception, e:
    4143            raise CommandError("Unable to serialize database: %s" % e)
  • 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:
     
    2019    """
    2120    Convert a queryset to JSON.
    2221    """
     22    def start_serialization(self):
     23        self._current = None
     24        self.json_kwargs = self.options.copy()
     25        self.json_kwargs.pop('stream', None)
     26        self.json_kwargs.pop('fields', None)
     27        self.stream.write("[")
     28
    2329    def end_serialization(self):
    24         self.options.pop('stream', None)
    25         self.options.pop('fields', None)
    26         simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder, **self.options)
     30        if self.options.get("indent"):
     31            self.stream.write("\n")
     32        self.stream.write("]\n")
    2733
     34    def end_object(self, obj):
     35        # self._current has the field data
     36        if not self.first:
     37            self.stream.write(",")
     38        if self.options.get("indent"):
     39            self.stream.write("\n")
     40        simplejson.dump(self.get_dump_object(obj), self.stream,
     41                        cls=DjangoJSONEncoder, **self.json_kwargs)
     42        self._current = None
     43
    2844    def getvalue(self):
     45        # overwrite PythonSerializer.getvalue() with base Serializer.getvalue()
    2946        if callable(getattr(self.stream, 'getvalue', None)):
    3047            return self.stream.getvalue()
    3148
     49
    3250def Deserializer(stream_or_string, **options):
    3351    """
    3452    Deserialize a stream or string of JSON data.
     
    4462    """
    4563    JSONEncoder subclass that knows how to encode date/time and decimal types.
    4664    """
    47 
    48     DATE_FORMAT = "%Y-%m-%d"
     65   
     66    DATE_FORMAT = "%d.%m.%Y"
    4967    TIME_FORMAT = "%H:%M:%S"
    5068
    5169    def default(self, o):
  • django/core/serializers/base.py

     
    3232        self.selected_fields = options.get("fields")
    3333
    3434        self.start_serialization()
     35        self.first = True
    3536        for obj in queryset:
    3637            self.start_object(obj)
    3738            for field in obj._meta.fields:
     
    4748                    if self.selected_fields is None or field.attname in self.selected_fields:
    4849                        self.handle_m2m_field(obj, field)
    4950            self.end_object(obj)
     51            if self.first:
     52                self.first = False
    5053        self.end_serialization()
    5154        return self.getvalue()
    5255
     
    5558        Convert a field's value to a string.
    5659        """
    5760        if isinstance(field, models.DateTimeField):
    58             value = getattr(obj, field.name).strftime("%Y-%m-%d %H:%M:%S")
     61            value = getattr(obj, field.name)
     62            if value is None:
     63                value = ''
     64            else:
     65                value = value.strftime("%d.%m.%Y %H:%M:%S")
    5966        elif isinstance(field, models.FileField):
    6067            value = getattr(obj, "get_%s_url" % field.name, lambda: None)()
    6168        else:
  • django/core/serializers/python.py

     
    2525        self._current = {}
    2626
    2727    def end_object(self, obj):
    28         self.objects.append({
     28        self.objects.append(self.get_dump_object(obj))
     29        self._current = None
     30
     31    def get_dump_object (self, obj):
     32        return {
    2933            "model"  : smart_unicode(obj._meta),
    3034            "pk"     : smart_unicode(obj._get_pk_val(), strings_only=True),
    3135            "fields" : self._current
    32         })
    33         self._current = None
     36        }
    3437
    3538    def handle_field(self, obj, field):
    3639        self._current[field.name] = smart_unicode(getattr(obj, field.name), strings_only=True)
Back to Top