diff --git a/django/core/management/commands/dumpdata.py b/django/core/management/commands/dumpdata.py
a
|
b
|
|
1 | 1 | from django.core.exceptions import ImproperlyConfigured |
2 | 2 | from django.core.management.base import BaseCommand, CommandError |
3 | 3 | from django.core import serializers |
4 | | from django.db import connections, router, DEFAULT_DB_ALIAS |
| 4 | from django.db import router, DEFAULT_DB_ALIAS |
5 | 5 | from django.utils.datastructures import SortedDict |
6 | 6 | |
7 | 7 | from optparse import make_option |
… |
… |
|
28 | 28 | args = '[appname appname.ModelName ...]' |
29 | 29 | |
30 | 30 | def handle(self, *app_labels, **options): |
31 | | from django.db.models import get_app, get_apps, get_models, get_model |
| 31 | from django.db.models import get_app, get_apps, get_model |
32 | 32 | |
33 | | format = options.get('format','json') |
34 | | indent = options.get('indent',None) |
| 33 | format = options.get('format', 'json') |
| 34 | indent = options.get('indent', None) |
35 | 35 | using = options.get('database', DEFAULT_DB_ALIAS) |
36 | | connection = connections[using] |
37 | | excludes = options.get('exclude',[]) |
| 36 | excludes = options.get('exclude', []) |
38 | 37 | show_traceback = options.get('traceback', False) |
39 | 38 | use_natural_keys = options.get('use_natural_keys', False) |
40 | 39 | use_base_manager = options.get('use_base_manager', False) |
… |
… |
|
98 | 97 | except KeyError: |
99 | 98 | raise CommandError("Unknown serialization format: %s" % format) |
100 | 99 | |
101 | | # Now collate the objects to be serialized. |
102 | | objects = [] |
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.extend(model._base_manager.using(using).all()) |
109 | | else: |
110 | | objects.extend(model._default_manager.using(using).all()) |
| 100 | def get_objects (): |
| 101 | # Collate the objects to be serialized. |
| 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 | for obj in model._base_manager.using(using).order_by(model._meta.pk.name).iterator(): |
| 108 | yield obj |
| 109 | else: |
| 110 | for obj in model._default_manager.using(using).order_by(model._meta.pk.name).iterator(): |
| 111 | yield obj |
111 | 112 | |
112 | 113 | try: |
113 | | return serializers.serialize(format, objects, indent=indent, |
114 | | use_natural_keys=use_natural_keys) |
| 114 | return serializers.serialize(format, get_objects(), indent=indent, |
| 115 | use_natural_keys=use_natural_keys) |
115 | 116 | except Exception, e: |
116 | 117 | if show_traceback: |
117 | 118 | raise |
diff --git a/django/core/serializers/base.py b/django/core/serializers/base.py
a
|
b
|
|
36 | 36 | self.use_natural_keys = options.pop("use_natural_keys", False) |
37 | 37 | |
38 | 38 | self.start_serialization() |
| 39 | self.first = True |
39 | 40 | for obj in queryset: |
40 | 41 | self.start_object(obj) |
41 | 42 | for field in obj._meta.local_fields: |
… |
… |
|
51 | 52 | if self.selected_fields is None or field.attname in self.selected_fields: |
52 | 53 | self.handle_m2m_field(obj, field) |
53 | 54 | self.end_object(obj) |
| 55 | if self.first: |
| 56 | self.first = False |
54 | 57 | self.end_serialization() |
55 | 58 | return self.getvalue() |
56 | 59 | |
diff --git a/django/core/serializers/json.py b/django/core/serializers/json.py
a
|
b
|
|
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") |
| 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 |
22 | 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 | |
diff --git a/django/core/serializers/python.py b/django/core/serializers/python.py
a
|
b
|
|
27 | 27 | self._current = {} |
28 | 28 | |
29 | 29 | 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 { |
31 | 35 | "model" : smart_unicode(obj._meta), |
32 | 36 | "pk" : smart_unicode(obj._get_pk_val(), strings_only=True), |
33 | 37 | "fields" : self._current |
34 | | }) |
35 | | self._current = None |
| 38 | } |
36 | 39 | |
37 | 40 | def handle_field(self, obj, field): |
38 | 41 | value = field._get_val_from_obj(obj) |