Ticket #5423: django_dumpdata_streamed_output.diff
File django_dumpdata_streamed_output.diff, 5.4 KB (added by , 17 years ago) |
---|
-
django/core/management/commands/dumpdata.py
1 1 from django.core.management.base import BaseCommand, CommandError 2 3 2 from optparse import make_option 3 import sys 4 4 5 5 6 class Command(BaseCommand): 6 7 option_list = BaseCommand.option_list + ( 7 8 make_option('--format', default='json', dest='format', … … 31 32 except KeyError: 32 33 raise CommandError("Unknown serialization format: %s" % format) 33 34 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 38 40 try: 39 return serializers.serialize(format, objects, indent=indent)41 serializers.serialize(format, get_objects(), indent=indent, stream=sys.stdout) 40 42 except Exception, e: 41 43 raise CommandError("Unable to serialize database: %s" % e) -
django/core/serializers/json.py
4 4 5 5 import datetime 6 6 from django.utils import simplejson 7 from django.utils.simplejson import decoder8 7 from django.core.serializers.python import Serializer as PythonSerializer 9 8 from django.core.serializers.python import Deserializer as PythonDeserializer 10 9 try: … … 20 19 """ 21 20 Convert a queryset to JSON. 22 21 """ 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 23 29 def end_serialization(self): 24 self.options.pop('stream', None)25 self.options.pop('fields', None)26 s implejson.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") 27 33 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 28 44 def getvalue(self): 45 # overwrite PythonSerializer.getvalue() with base Serializer.getvalue() 29 46 if callable(getattr(self.stream, 'getvalue', None)): 30 47 return self.stream.getvalue() 31 48 49 32 50 def Deserializer(stream_or_string, **options): 33 51 """ 34 52 Deserialize a stream or string of JSON data. … … 44 62 """ 45 63 JSONEncoder subclass that knows how to encode date/time and decimal types. 46 64 """ 47 48 DATE_FORMAT = "% Y-%m-%d"65 66 DATE_FORMAT = "%d.%m.%Y" 49 67 TIME_FORMAT = "%H:%M:%S" 50 68 51 69 def default(self, o): -
django/core/serializers/base.py
32 32 self.selected_fields = options.get("fields") 33 33 34 34 self.start_serialization() 35 self.first = True 35 36 for obj in queryset: 36 37 self.start_object(obj) 37 38 for field in obj._meta.fields: … … 47 48 if self.selected_fields is None or field.attname in self.selected_fields: 48 49 self.handle_m2m_field(obj, field) 49 50 self.end_object(obj) 51 if self.first: 52 self.first = False 50 53 self.end_serialization() 51 54 return self.getvalue() 52 55 … … 55 58 Convert a field's value to a string. 56 59 """ 57 60 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") 59 66 elif isinstance(field, models.FileField): 60 67 value = getattr(obj, "get_%s_url" % field.name, lambda: None)() 61 68 else: -
django/core/serializers/python.py
25 25 self._current = {} 26 26 27 27 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 { 29 33 "model" : smart_unicode(obj._meta), 30 34 "pk" : smart_unicode(obj._get_pk_val(), strings_only=True), 31 35 "fields" : self._current 32 }) 33 self._current = None 36 } 34 37 35 38 def handle_field(self, obj, field): 36 39 self._current[field.name] = smart_unicode(getattr(obj, field.name), strings_only=True)