Ticket #5423: 5423.4.diff
File 5423.4.diff, 5.7 KB (added by , 13 years ago) |
---|
-
django/core/serializers/json.py
17 17 """ 18 18 internal_use_only = False 19 19 20 def start_serialization(self): 21 self._current = None 22 self.json_kwargs = self.options.copy() 23 self.json_kwargs.pop('stream', None) 24 self.json_kwargs.pop('fields', None) 25 self.stream.write("[") 26 20 27 def end_serialization(self): 21 simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder, **self.options) 28 if self.options.get("indent"): 29 self.stream.write("\n") 30 self.stream.write("]") 31 if self.options.get("indent"): 32 self.stream.write("\n") 22 33 34 def end_object(self, obj): 35 # self._current has the field data 36 indent = self.options.get("indent") 37 if not self.first: 38 self.stream.write(",") 39 if not indent: 40 self.stream.write(" ") 41 if indent: 42 self.stream.write("\n") 43 simplejson.dump(self.get_dump_object(obj), self.stream, 44 cls=DjangoJSONEncoder, **self.json_kwargs) 45 self._current = None 46 23 47 def getvalue(self): 48 # overwrite PythonSerializer.getvalue() with base Serializer.getvalue() 24 49 if callable(getattr(self.stream, 'getvalue', None)): 25 50 return self.stream.getvalue() 26 51 -
django/core/serializers/base.py
39 39 self.use_natural_keys = options.pop("use_natural_keys", False) 40 40 41 41 self.start_serialization() 42 self.first = True 42 43 for obj in queryset: 43 44 self.start_object(obj) 44 45 for field in obj._meta.local_fields: … … 54 55 if self.selected_fields is None or field.attname in self.selected_fields: 55 56 self.handle_m2m_field(obj, field) 56 57 self.end_object(obj) 58 if self.first: 59 self.first = False 57 60 self.end_serialization() 58 61 return self.getvalue() 59 62 -
django/core/serializers/python.py
27 27 self._current = {} 28 28 29 29 def end_object(self, obj): 30 self.objects.append({ 31 "model" : smart_unicode(obj._meta), 32 "pk" : smart_unicode(obj._get_pk_val(), strings_only=True), 33 "fields" : self._current 34 }) 30 self.objects.append(self.get_dump_object(obj)) 35 31 self._current = None 36 32 33 def get_dump_object(self, obj): 34 return { 35 "pk": smart_unicode(obj._get_pk_val(), strings_only=True), 36 "model": smart_unicode(obj._meta), 37 "fields": self._current 38 } 39 37 40 def handle_field(self, obj, field): 38 41 value = field._get_val_from_obj(obj) 39 42 # Protected types (i.e., primitives like None, numbers, dates, -
django/core/management/commands/dumpdata.py
4 4 from django.db import router, DEFAULT_DB_ALIAS 5 5 from django.utils.datastructures import SortedDict 6 6 7 import sys 7 8 from optparse import make_option 8 9 9 10 class Command(BaseCommand): … … 30 31 def handle(self, *app_labels, **options): 31 32 from django.db.models import get_app, get_apps, get_model 32 33 33 format = options.get('format', 'json')34 indent = options.get('indent', None)34 format = options.get('format', 'json') 35 indent = options.get('indent', None) 35 36 using = options.get('database', DEFAULT_DB_ALIAS) 36 37 excludes = options.get('exclude',[]) 37 38 show_traceback = options.get('traceback', False) … … 97 98 except KeyError: 98 99 raise CommandError("Unknown serialization format: %s" % format) 99 100 100 # Now collate the objects to be serialized. 101 objects = [] 102 for model in sort_dependencies(app_list.items()): 103 if model in excluded_models: 104 continue 105 if not model._meta.proxy and router.allow_syncdb(using, model): 106 if use_base_manager: 107 objects.extend(model._base_manager.using(using).all()) 108 else: 109 objects.extend(model._default_manager.using(using).all()) 101 def get_objects(): 102 # Collate the objects to be serialized. 103 for model in sort_dependencies(app_list.items()): 104 if model in excluded_models: 105 continue 106 if not model._meta.proxy and router.allow_syncdb(using, model): 107 if use_base_manager: 108 objects = model._base_manager 109 else: 110 objects = model._default_manager 111 for obj in objects.using(using).\ 112 order_by(model._meta.pk.name).iterator(): 113 yield obj 110 114 111 115 try: 112 return serializers.serialize(format, objects, indent=indent,113 use_natural_keys=use_natural_keys)116 serializers.serialize(format, get_objects(), indent=indent, 117 use_natural_keys=use_natural_keys, stream=sys.stdout) 114 118 except Exception, e: 115 119 if show_traceback: 116 120 raise