Ticket #5423: django_dumpdata_streamed_output_2.diff
File django_dumpdata_streamed_output_2.diff, 4.8 KB (added by , 17 years ago) |
---|
-
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. -
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 -
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 self._current[field.name] = smart_unicode(getattr(obj, field.name), strings_only=True) 39 value = self.get_string_value(obj, field) 40 self._current[field.name] = smart_unicode(value) 37 41 38 42 def handle_fk_field(self, obj, field): 39 43 related = getattr(obj, field.name) -
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.order_by(model._meta.pk.attname).iterator(): 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)