diff -r d834ef768f04 django/core/management/commands/dumpdata.py
a
|
b
|
|
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): |
… |
… |
|
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 |
diff -r d834ef768f04 django/core/serializers/base.py
a
|
b
|
|
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 | |
diff -r d834ef768f04 django/core/serializers/json.py
a
|
b
|
|
17 | 17 | """ |
18 | 18 | internal_use_only = False |
19 | 19 | |
20 | | def end_serialization(self): |
| 20 | def start_serialization(self): |
21 | 21 | if simplejson.__version__.split('.') >= ['2', '1', '3']: |
22 | 22 | # Use JS strings to represent Python Decimal instances (ticket #16850) |
23 | 23 | self.options.update({'use_decimal': False}) |
24 | | simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder, **self.options) |
| 24 | self._current = None |
| 25 | self.json_kwargs = self.options.copy() |
| 26 | self.json_kwargs.pop('stream', None) |
| 27 | self.json_kwargs.pop('fields', None) |
| 28 | self.stream.write("[") |
| 29 | |
| 30 | def end_serialization(self): |
| 31 | if self.options.get("indent"): |
| 32 | self.stream.write("\n") |
| 33 | self.stream.write("]") |
| 34 | if self.options.get("indent"): |
| 35 | self.stream.write("\n") |
| 36 | |
| 37 | def end_object(self, obj): |
| 38 | # self._current has the field data |
| 39 | indent = self.options.get("indent") |
| 40 | if not self.first: |
| 41 | self.stream.write(",") |
| 42 | if not indent: |
| 43 | self.stream.write(" ") |
| 44 | if indent: |
| 45 | self.stream.write("\n") |
| 46 | simplejson.dump(self.get_dump_object(obj), self.stream, |
| 47 | cls=DjangoJSONEncoder, **self.json_kwargs) |
| 48 | self._current = None |
25 | 49 | |
26 | 50 | def getvalue(self): |
| 51 | # overwrite PythonSerializer.getvalue() with base Serializer.getvalue() |
27 | 52 | if callable(getattr(self.stream, 'getvalue', None)): |
28 | 53 | return self.stream.getvalue() |
29 | 54 | |
diff -r d834ef768f04 django/core/serializers/python.py
a
|
b
|
|
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, |