| 1 |
import gzip |
| 2 |
import os |
| 3 |
import warnings |
| 4 |
|
| 5 |
from django.apps import apps |
| 6 |
from django.core import serializers |
| 7 |
from django.core.management.base import BaseCommand, CommandError |
| 8 |
from django.core.management.utils import parse_apps_and_model_labels |
| 9 |
from django.db import DEFAULT_DB_ALIAS, connections, router |
| 10 |
|
| 11 |
try: |
| 12 |
import bz2 |
| 13 |
|
| 14 |
has_bz2 = True |
| 15 |
except ImportError: |
| 16 |
has_bz2 = False |
| 17 |
|
| 18 |
try: |
| 19 |
import lzma |
| 20 |
|
| 21 |
has_lzma = True |
| 22 |
except ImportError: |
| 23 |
has_lzma = False |
| 24 |
|
| 25 |
|
| 26 |
class ProxyModelWarning(Warning): |
| 27 |
pass |
| 28 |
|
| 29 |
|
| 30 |
class Command(BaseCommand): |
| 31 |
help = ( |
| 32 |
"Output the contents of the database as a fixture of the given format " |
| 33 |
"(using each model's default manager unless --all is specified)." |
| 34 |
) |
| 35 |
|
| 36 |
def add_arguments(self, parser): |
| 37 |
parser.add_argument( |
| 38 |
"args", |
| 39 |
metavar="app_label[.ModelName]", |
| 40 |
nargs="*", |
| 41 |
help=( |
| 42 |
"Restricts dumped data to the specified app_label or " |
| 43 |
"app_label.ModelName." |
| 44 |
), |
| 45 |
) |
| 46 |
parser.add_argument( |
| 47 |
"--format", |
| 48 |
default="json", |
| 49 |
help="Specifies the output serialization format for fixtures.", |
| 50 |
) |
| 51 |
parser.add_argument( |
| 52 |
"--indent", |
| 53 |
type=int, |
| 54 |
help="Specifies the indent level to use when pretty-printing output.", |
| 55 |
) |
| 56 |
parser.add_argument( |
| 57 |
"--database", |
| 58 |
default=DEFAULT_DB_ALIAS, |
| 59 |
choices=tuple(connections), |
| 60 |
help="Nominates a specific database to dump fixtures from. " |
| 61 |
'Defaults to the "default" database.', |
| 62 |
) |
| 63 |
parser.add_argument( |
| 64 |
"-e", |
| 65 |
"--exclude", |
| 66 |
action="append", |
| 67 |
default=[], |
| 68 |
help="An app_label or app_label.ModelName to exclude " |
| 69 |
"(use multiple --exclude to exclude multiple apps/models).", |
| 70 |
) |
| 71 |
parser.add_argument( |
| 72 |
"--natural-foreign", |
| 73 |
action="store_true", |
| 74 |
dest="use_natural_foreign_keys", |
| 75 |
help="Use natural foreign keys if they are available.", |
| 76 |
) |
| 77 |
parser.add_argument( |
| 78 |
"--natural-primary", |
| 79 |
action="store_true", |
| 80 |
dest="use_natural_primary_keys", |
| 81 |
help="Use natural primary keys if they are available.", |
| 82 |
) |
| 83 |
parser.add_argument( |
| 84 |
"-a", |
| 85 |
"--all", |
| 86 |
action="store_true", |
| 87 |
dest="use_base_manager", |
| 88 |
help=( |
| 89 |
"Use Django's base manager to dump all models stored in the database, " |
| 90 |
"including those that would otherwise be filtered or modified by a " |
| 91 |
"custom manager." |
| 92 |
), |
| 93 |
) |
| 94 |
parser.add_argument( |
| 95 |
"--pks", |
| 96 |
dest="primary_keys", |
| 97 |
help="Only dump objects with given primary keys. Accepts a comma-separated " |
| 98 |
"list of keys. This option only works when you specify one model.", |
| 99 |
) |
| 100 |
parser.add_argument( |
| 101 |
"-o", "--output", help="Specifies file to which the output is written." |
| 102 |
) |
| 103 |
|
| 104 |
def handle(self, *app_labels, **options): |
| 105 |
format = options["format"] |
| 106 |
indent = options["indent"] |
| 107 |
using = options["database"] |
| 108 |
excludes = options["exclude"] |
| 109 |
output = options["output"] |
| 110 |
show_traceback = options["traceback"] |
| 111 |
use_natural_foreign_keys = options["use_natural_foreign_keys"] |
| 112 |
use_natural_primary_keys = options["use_natural_primary_keys"] |
| 113 |
use_base_manager = options["use_base_manager"] |
| 114 |
pks = options["primary_keys"] |
| 115 |
|
| 116 |
if pks: |
| 117 |
primary_keys = [pk.strip() for pk in pks.split(",")] |
| 118 |
else: |
| 119 |
primary_keys = [] |
| 120 |
|
| 121 |
excluded_models, excluded_apps = parse_apps_and_model_labels(excludes) |
| 122 |
|
| 123 |
if not app_labels: |
| 124 |
if primary_keys: |
| 125 |
raise CommandError("You can only use --pks option with one model") |
| 126 |
app_list = dict.fromkeys( |
| 127 |
app_config |
| 128 |
for app_config in apps.get_app_configs() |
| 129 |
if app_config.models_module is not None |
| 130 |
and app_config not in excluded_apps |
| 131 |
) |
| 132 |
else: |
| 133 |
if len(app_labels) > 1 and primary_keys: |
| 134 |
raise CommandError("You can only use --pks option with one model") |
| 135 |
app_list = {} |
| 136 |
for label in app_labels: |
| 137 |
try: |
| 138 |
app_label, model_label = label.split(".") |
| 139 |
try: |
| 140 |
app_config = apps.get_app_config(app_label) |
| 141 |
except LookupError as e: |
| 142 |
raise CommandError(str(e)) |
| 143 |
if app_config.models_module is None or app_config in excluded_apps: |
| 144 |
continue |
| 145 |
try: |
| 146 |
model = app_config.get_model(model_label) |
| 147 |
except LookupError: |
| 148 |
raise CommandError( |
| 149 |
"Unknown model: %s.%s" % (app_label, model_label) |
| 150 |
) |
| 151 |
|
| 152 |
app_list_value = app_list.setdefault(app_config, []) |
| 153 |
|
| 154 |
# We may have previously seen an "all-models" request for |
| 155 |
# this app (no model qualifier was given). In this case |
| 156 |
# there is no need adding specific models to the list. |
| 157 |
if app_list_value is not None and model not in app_list_value: |
| 158 |
app_list_value.append(model) |
| 159 |
except ValueError: |
| 160 |
if primary_keys: |
| 161 |
raise CommandError( |
| 162 |
"You can only use --pks option with one model" |
| 163 |
) |
| 164 |
# This is just an app - no model qualifier |
| 165 |
app_label = label |
| 166 |
try: |
| 167 |
app_config = apps.get_app_config(app_label) |
| 168 |
except LookupError as e: |
| 169 |
raise CommandError(str(e)) |
| 170 |
if app_config.models_module is None or app_config in excluded_apps: |
| 171 |
continue |
| 172 |
app_list[app_config] = None |
| 173 |
|
| 174 |
# Check that the serialization format exists; this is a shortcut to |
| 175 |
# avoid collating all the objects and _then_ failing. |
| 176 |
if format not in serializers.get_public_serializer_formats(): |
| 177 |
try: |
| 178 |
serializers.get_serializer(format) |
| 179 |
except serializers.SerializerDoesNotExist: |
| 180 |
pass |
| 181 |
|
| 182 |
raise CommandError("Unknown serialization format: %s" % format) |
| 183 |
|
| 184 |
def get_objects(count_only=False): |
| 185 |
""" |
| 186 |
Collate the objects to be serialized. If count_only is True, just |
| 187 |
count the number of objects to be serialized. |
| 188 |
""" |
| 189 |
if use_natural_foreign_keys: |
| 190 |
models = serializers.sort_dependencies( |
| 191 |
app_list.items(), allow_cycles=True |
| 192 |
) |
| 193 |
else: |
| 194 |
# There is no need to sort dependencies when natural foreign |
| 195 |
# keys are not used. |
| 196 |
models = [] |
| 197 |
for app_config, model_list in app_list.items(): |
| 198 |
if model_list is None: |
| 199 |
models.extend(app_config.get_models()) |
| 200 |
else: |
| 201 |
models.extend(model_list) |
| 202 |
for model in models: |
| 203 |
if model in excluded_models: |
| 204 |
continue |
| 205 |
if model._meta.proxy and model._meta.proxy_for_model not in models: |
| 206 |
warnings.warn( |
| 207 |
"%s is a proxy model and won't be serialized." |
| 208 |
% model._meta.label, |
| 209 |
category=ProxyModelWarning, |
| 210 |
) |
| 211 |
if not model._meta.proxy and router.allow_migrate_model(using, model): |
| 212 |
if use_base_manager: |
| 213 |
objects = model._base_manager |
| 214 |
else: |
| 215 |
objects = model._default_manager |
| 216 |
|
| 217 |
queryset = objects.using(using).order_by(model._meta.pk.name) |
| 218 |
if primary_keys: |
| 219 |
queryset = queryset.filter(pk__in=primary_keys) |
| 220 |
if count_only: |
| 221 |
yield queryset.order_by().count() |
| 222 |
else: |
| 223 |
chunk_size = ( |
| 224 |
2000 if queryset._prefetch_related_lookups else None |
| 225 |
) |
| 226 |
yield from queryset.iterator(chunk_size=chunk_size) |
| 227 |
|
| 228 |
try: |
| 229 |
self.stdout.ending = None |
| 230 |
progress_output = None |
| 231 |
object_count = 0 |
| 232 |
# If dumpdata is outputting to stdout, there is no way to display progress |
| 233 |
if output and self.stdout.isatty() and options["verbosity"] > 0: |
| 234 |
progress_output = self.stdout |
| 235 |
object_count = sum(get_objects(count_only=True)) |
| 236 |
if output: |
| 237 |
file_root, file_ext = os.path.splitext(output) |
| 238 |
compression_formats = { |
| 239 |
".bz2": (open, {}, file_root), |
| 240 |
".gz": (gzip.open, {}, output), |
| 241 |
".lzma": (open, {}, file_root), |
| 242 |
".xz": (open, {}, file_root), |
| 243 |
".zip": (open, {}, file_root), |
| 244 |
} |
| 245 |
if has_bz2: |
| 246 |
compression_formats[".bz2"] = (bz2.open, {}, output) |
| 247 |
if has_lzma: |
| 248 |
compression_formats[".lzma"] = ( |
| 249 |
lzma.open, |
| 250 |
{"format": lzma.FORMAT_ALONE}, |
| 251 |
output, |
| 252 |
) |
| 253 |
compression_formats[".xz"] = (lzma.open, {}, output) |
| 254 |
try: |
| 255 |
open_method, kwargs, file_path = compression_formats[file_ext] |
| 256 |
except KeyError: |
| 257 |
open_method, kwargs, file_path = (open, {}, output) |
| 258 |
if file_path != output: |
| 259 |
file_name = os.path.basename(file_path) |
| 260 |
warnings.warn( |
| 261 |
f"Unsupported file extension ({file_ext}). " |
| 262 |
f"Fixtures saved in '{file_name}'.", |
| 263 |
RuntimeWarning, |
| 264 |
) |
| 265 |
stream = open_method(file_path, "wt", **kwargs) |
| 266 |
else: |
| 267 |
stream = None |
| 268 |
try: |
| 269 |
serializers.serialize( |
| 270 |
format, |
| 271 |
get_objects(), |
| 272 |
indent=indent, |
| 273 |
use_natural_foreign_keys=use_natural_foreign_keys, |
| 274 |
use_natural_primary_keys=use_natural_primary_keys, |
| 275 |
stream=stream or self.stdout, |
| 276 |
progress_output=progress_output, |
| 277 |
object_count=object_count, |
| 278 |
) |
| 279 |
finally: |
| 280 |
if stream: |
| 281 |
stream.close() |
| 282 |
except Exception as e: |
| 283 |
if show_traceback: |
| 284 |
raise |
| 285 |
raise CommandError("Unable to serialize database: %s" % e) |