summaryrefslogtreecommitdiff
path: root/django/core/management/utils.py
diff options
context:
space:
mode:
authorBerker Peksag <berker.peksag@gmail.com>2016-05-17 09:52:01 +0300
committerTim Graham <timograham@gmail.com>2016-06-09 10:35:32 -0400
commitae2a7da86bf841d42be86dea3effb0261187c950 (patch)
tree563af5761dfe6f02b4f4ca14c9500a94e1a0f5ee /django/core/management/utils.py
parent21130ce1a9c4fcbfce4c614be9e5408b43092bf0 (diff)
downloaddjango-ae2a7da86bf841d42be86dea3effb0261187c950.tar.gz
Fixed #20468 -- Added loaddata --exclude option.
Thanks Alex Morozov for the initial patch.
Diffstat (limited to 'django/core/management/utils.py')
-rw-r--r--django/core/management/utils.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/django/core/management/utils.py b/django/core/management/utils.py
index 637091e68a..7ea005a6f0 100644
--- a/django/core/management/utils.py
+++ b/django/core/management/utils.py
@@ -4,6 +4,7 @@ import os
import sys
from subprocess import PIPE, Popen
+from django.apps import apps as installed_apps
from django.utils import six
from django.utils.crypto import get_random_string
from django.utils.encoding import DEFAULT_LOCALE_ENCODING, force_text
@@ -84,3 +85,30 @@ def get_random_secret_key():
"""
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
return get_random_string(50, chars)
+
+
+def parse_apps_and_model_labels(labels):
+ """
+ Parse a list of "app_label.ModelName" or "app_label" strings into actual
+ objects and return a two-element tuple:
+ (set of model classes, set of app_configs).
+ Raise a CommandError if some specified models or apps don't exist.
+ """
+ apps = set()
+ models = set()
+
+ for label in labels:
+ if '.' in label:
+ try:
+ model = installed_apps.get_model(label)
+ except LookupError:
+ raise CommandError('Unknown model: %s' % label)
+ models.add(model)
+ else:
+ try:
+ app_config = installed_apps.get_app_config(label)
+ except LookupError as e:
+ raise CommandError(str(e))
+ apps.add(app_config)
+
+ return models, apps