Refactored dumpdata with app configs instead of models modules.
This commit is contained in:
parent
40b8767635
commit
efddae252c
|
@ -3,6 +3,7 @@ import warnings
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from optparse import make_option
|
from optparse import make_option
|
||||||
|
|
||||||
|
from django.apps import apps
|
||||||
from django.core.management.base import BaseCommand, CommandError
|
from django.core.management.base import BaseCommand, CommandError
|
||||||
from django.core import serializers
|
from django.core import serializers
|
||||||
from django.db import router, DEFAULT_DB_ALIAS
|
from django.db import router, DEFAULT_DB_ALIAS
|
||||||
|
@ -37,8 +38,6 @@ class Command(BaseCommand):
|
||||||
args = '[appname appname.ModelName ...]'
|
args = '[appname appname.ModelName ...]'
|
||||||
|
|
||||||
def handle(self, *app_labels, **options):
|
def handle(self, *app_labels, **options):
|
||||||
from django.apps import apps
|
|
||||||
|
|
||||||
format = options.get('format')
|
format = options.get('format')
|
||||||
indent = options.get('indent')
|
indent = options.get('indent')
|
||||||
using = options.get('database')
|
using = options.get('database')
|
||||||
|
@ -63,24 +62,23 @@ class Command(BaseCommand):
|
||||||
for exclude in excludes:
|
for exclude in excludes:
|
||||||
if '.' in exclude:
|
if '.' in exclude:
|
||||||
app_label, model_name = exclude.split('.', 1)
|
app_label, model_name = exclude.split('.', 1)
|
||||||
model_obj = apps.get_model(app_label, model_name)
|
model = apps.get_model(app_label, model_name)
|
||||||
if not model_obj:
|
if not model:
|
||||||
raise CommandError('Unknown model in excludes: %s' % exclude)
|
raise CommandError('Unknown model in excludes: %s' % exclude)
|
||||||
excluded_models.add(model_obj)
|
excluded_models.add(model)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
app_obj = apps.get_app_config(exclude).models_module
|
app_config = apps.get_app_config(exclude)
|
||||||
if app_obj is not None:
|
|
||||||
excluded_apps.add(app_obj)
|
|
||||||
except LookupError:
|
except LookupError:
|
||||||
raise CommandError('Unknown app in excludes: %s' % exclude)
|
raise CommandError('Unknown app in excludes: %s' % exclude)
|
||||||
|
excluded_apps.add(app_config)
|
||||||
|
|
||||||
if len(app_labels) == 0:
|
if len(app_labels) == 0:
|
||||||
if primary_keys:
|
if primary_keys:
|
||||||
raise CommandError("You can only use --pks option with one model")
|
raise CommandError("You can only use --pks option with one model")
|
||||||
app_list = OrderedDict((app_config.models_module, None)
|
app_list = OrderedDict((app_config, None)
|
||||||
for app_config in apps.get_app_configs(only_with_models_module=True)
|
for app_config in apps.get_app_configs(only_with_models_module=True)
|
||||||
if app_config.models_module not in excluded_apps)
|
if app_config not in excluded_apps)
|
||||||
else:
|
else:
|
||||||
if len(app_labels) > 1 and primary_keys:
|
if len(app_labels) > 1 and primary_keys:
|
||||||
raise CommandError("You can only use --pks option with one model")
|
raise CommandError("You can only use --pks option with one model")
|
||||||
|
@ -89,32 +87,30 @@ class Command(BaseCommand):
|
||||||
try:
|
try:
|
||||||
app_label, model_label = label.split('.')
|
app_label, model_label = label.split('.')
|
||||||
try:
|
try:
|
||||||
app = apps.get_app_config(app_label).models_module
|
app_config = apps.get_app_config(app_label)
|
||||||
except LookupError:
|
except LookupError:
|
||||||
raise CommandError("Unknown application: %s" % app_label)
|
raise CommandError("Unknown application: %s" % app_label)
|
||||||
if app is None or app in excluded_apps:
|
if app_config.models_module is None or app_config in excluded_apps:
|
||||||
continue
|
continue
|
||||||
model = apps.get_model(app_label, model_label)
|
model = apps.get_model(app_label, model_label)
|
||||||
if model is None:
|
if model is None:
|
||||||
raise CommandError("Unknown model: %s.%s" % (app_label, model_label))
|
raise CommandError("Unknown model: %s.%s" % (app_label, model_label))
|
||||||
|
|
||||||
if app in app_list.keys():
|
app_list_value = app_list.setdefault(app_config, [])
|
||||||
if app_list[app] and model not in app_list[app]:
|
if model not in app_list_value:
|
||||||
app_list[app].append(model)
|
app_list_value.append(model)
|
||||||
else:
|
|
||||||
app_list[app] = [model]
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
if primary_keys:
|
if primary_keys:
|
||||||
raise CommandError("You can only use --pks option with one model")
|
raise CommandError("You can only use --pks option with one model")
|
||||||
# This is just an app - no model qualifier
|
# This is just an app - no model qualifier
|
||||||
app_label = label
|
app_label = label
|
||||||
try:
|
try:
|
||||||
app = apps.get_app_config(app_label).models_module
|
app_config = apps.get_app_config(app_label)
|
||||||
except LookupError:
|
except LookupError:
|
||||||
raise CommandError("Unknown application: %s" % app_label)
|
raise CommandError("Unknown application: %s" % app_label)
|
||||||
if app is None or app in excluded_apps:
|
if app_config.models_module is None or app_config in excluded_apps:
|
||||||
continue
|
continue
|
||||||
app_list[app] = None
|
app_list[app_config] = None
|
||||||
|
|
||||||
# Check that the serialization format exists; this is a shortcut to
|
# Check that the serialization format exists; this is a shortcut to
|
||||||
# avoid collating all the objects and _then_ failing.
|
# avoid collating all the objects and _then_ failing.
|
||||||
|
@ -156,19 +152,18 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
|
|
||||||
def sort_dependencies(app_list):
|
def sort_dependencies(app_list):
|
||||||
"""Sort a list of app,modellist pairs into a single list of models.
|
"""Sort a list of (app_config, models) pairs into a single list of models.
|
||||||
|
|
||||||
The single list of models is sorted so that any model with a natural key
|
The single list of models is sorted so that any model with a natural key
|
||||||
is serialized before a normal model, and any model with a natural key
|
is serialized before a normal model, and any model with a natural key
|
||||||
dependency has it's dependencies serialized first.
|
dependency has it's dependencies serialized first.
|
||||||
"""
|
"""
|
||||||
from django.apps import apps
|
|
||||||
# Process the list of models, and get the list of dependencies
|
# Process the list of models, and get the list of dependencies
|
||||||
model_dependencies = []
|
model_dependencies = []
|
||||||
models = set()
|
models = set()
|
||||||
for app, model_list in app_list:
|
for app_config, model_list in app_list:
|
||||||
if model_list is None:
|
if model_list is None:
|
||||||
model_list = apps.get_models(app)
|
model_list = apps.get_models(app_config.models_module)
|
||||||
|
|
||||||
for model in model_list:
|
for model in model_list:
|
||||||
models.add(model)
|
models.add(model)
|
||||||
|
|
Loading…
Reference in New Issue