mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Removed module-level functions for the app cache.
Since the original ones in django.db.models.loading were kept only for backwards compatibility, there's no need to recreate them. However, many internals of Django still relied on them. They were also imported in django.db.models. They never appear in the documentation, except a quick mention of get_models and get_app in the 1.2 release notes to document an edge case in GIS. I don't think that makes them a public API. This commit doesn't change the overall amount of global state but clarifies that it's tied to the app_cache object instead of hiding it behind half a dozen functions.
This commit is contained in:
@@ -0,0 +1 @@
|
||||
from .cache import app_cache, UnavailableApp # NOQA
|
||||
|
||||
@@ -12,8 +12,6 @@ from django.utils.module_loading import module_has_submodule
|
||||
from django.utils._os import upath
|
||||
from django.utils import six
|
||||
|
||||
__all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models',
|
||||
'load_app', 'app_cache_ready')
|
||||
|
||||
MODELS_MODULE_NAME = 'models'
|
||||
|
||||
@@ -361,19 +359,4 @@ class AppCache(BaseAppCache):
|
||||
self.__dict__ = self.__shared_state
|
||||
|
||||
|
||||
cache = AppCache()
|
||||
|
||||
|
||||
# These methods were always module level, so are kept that way for backwards
|
||||
# compatibility.
|
||||
get_apps = cache.get_apps
|
||||
get_app_package = cache.get_app_package
|
||||
get_app_path = cache.get_app_path
|
||||
get_app_paths = cache.get_app_paths
|
||||
get_app = cache.get_app
|
||||
get_app_errors = cache.get_app_errors
|
||||
get_models = cache.get_models
|
||||
get_model = cache.get_model
|
||||
register_models = cache.register_models
|
||||
load_app = cache.load_app
|
||||
app_cache_ready = cache.app_cache_ready
|
||||
app_cache = AppCache()
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from django.apps import app_cache
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.db import models
|
||||
from django.db.models.fields import FieldDoesNotExist
|
||||
@@ -17,7 +18,7 @@ class BaseValidator(object):
|
||||
def __init__(self):
|
||||
# Before we can introspect models, they need to be fully loaded so that
|
||||
# inter-relations are set up correctly. We force that here.
|
||||
models.get_apps()
|
||||
app_cache.get_apps()
|
||||
|
||||
def validate(self, cls, model):
|
||||
for m in dir(self):
|
||||
|
||||
@@ -4,6 +4,7 @@ import os
|
||||
import re
|
||||
|
||||
from django import template
|
||||
from django.apps import app_cache
|
||||
from django.conf import settings
|
||||
from django.contrib import admin
|
||||
from django.contrib.admin.views.decorators import staff_member_required
|
||||
@@ -182,7 +183,7 @@ class ModelIndexView(BaseAdminDocsView):
|
||||
template_name = 'admin_doc/model_index.html'
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
m_list = [m._meta for m in models.get_models()]
|
||||
m_list = [m._meta for m in app_cache.get_models()]
|
||||
kwargs.update({'models': m_list})
|
||||
return super(ModelIndexView, self).get_context_data(**kwargs)
|
||||
|
||||
@@ -193,11 +194,11 @@ class ModelDetailView(BaseAdminDocsView):
|
||||
def get_context_data(self, **kwargs):
|
||||
# Get the model class.
|
||||
try:
|
||||
app_mod = models.get_app(self.kwargs['app_label'])
|
||||
app_mod = app_cache.get_app(self.kwargs['app_label'])
|
||||
except ImproperlyConfigured:
|
||||
raise Http404(_("App %r not found") % self.kwargs['app_label'])
|
||||
model = None
|
||||
for m in models.get_models(app_mod):
|
||||
for m in app_cache.get_models(app_mod):
|
||||
if m._meta.model_name == self.kwargs['model_name']:
|
||||
model = m
|
||||
break
|
||||
|
||||
@@ -123,13 +123,13 @@ def get_user_model():
|
||||
"""
|
||||
Returns the User model that is active in this project.
|
||||
"""
|
||||
from django.db.models import get_model
|
||||
from django.apps import app_cache
|
||||
|
||||
try:
|
||||
app_label, model_name = settings.AUTH_USER_MODEL.split('.')
|
||||
except ValueError:
|
||||
raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form 'app_label.model_name'")
|
||||
user_model = get_model(app_label, model_name)
|
||||
user_model = app_cache.get_model(app_label, model_name)
|
||||
if user_model is None:
|
||||
raise ImproperlyConfigured("AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL)
|
||||
return user_model
|
||||
|
||||
@@ -6,12 +6,13 @@ from __future__ import unicode_literals
|
||||
import getpass
|
||||
import unicodedata
|
||||
|
||||
from django.apps import app_cache, UnavailableApp
|
||||
from django.contrib.auth import (models as auth_app, get_permission_codename,
|
||||
get_user_model)
|
||||
from django.core import exceptions
|
||||
from django.core.management.base import CommandError
|
||||
from django.db import DEFAULT_DB_ALIAS, router
|
||||
from django.db.models import get_model, get_models, signals, UnavailableApp
|
||||
from django.db.models import signals
|
||||
from django.utils.encoding import DEFAULT_LOCALE_ENCODING
|
||||
from django.utils import six
|
||||
from django.utils.six.moves import input
|
||||
@@ -61,7 +62,7 @@ def _check_permission_clashing(custom, builtin, ctype):
|
||||
|
||||
def create_permissions(app, created_models, verbosity, db=DEFAULT_DB_ALIAS, **kwargs):
|
||||
try:
|
||||
get_model('auth', 'Permission')
|
||||
app_cache.get_model('auth', 'Permission')
|
||||
except UnavailableApp:
|
||||
return
|
||||
|
||||
@@ -70,7 +71,7 @@ def create_permissions(app, created_models, verbosity, db=DEFAULT_DB_ALIAS, **kw
|
||||
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
||||
app_models = get_models(app)
|
||||
app_models = app_cache.get_models(app)
|
||||
|
||||
# This will hold the permissions we're looking for as
|
||||
# (content_type, (codename, name))
|
||||
@@ -119,7 +120,7 @@ def create_permissions(app, created_models, verbosity, db=DEFAULT_DB_ALIAS, **kw
|
||||
|
||||
def create_superuser(app, created_models, verbosity, db, **kwargs):
|
||||
try:
|
||||
get_model('auth', 'Permission')
|
||||
app_cache.get_model('auth', 'Permission')
|
||||
UserModel = get_user_model()
|
||||
except UnavailableApp:
|
||||
return
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from __future__ import unicode_literals
|
||||
from datetime import date
|
||||
|
||||
from django.apps.cache import get_app
|
||||
from django.apps import app_cache
|
||||
from django.contrib.auth import models, management
|
||||
from django.contrib.auth.management import create_permissions
|
||||
from django.contrib.auth.management.commands import changepassword
|
||||
@@ -184,21 +184,21 @@ class CustomUserModelValidationTestCase(TestCase):
|
||||
def test_required_fields_is_list(self):
|
||||
"REQUIRED_FIELDS should be a list."
|
||||
new_io = StringIO()
|
||||
get_validation_errors(new_io, get_app('auth'))
|
||||
get_validation_errors(new_io, app_cache.get_app('auth'))
|
||||
self.assertIn("The REQUIRED_FIELDS must be a list or tuple.", new_io.getvalue())
|
||||
|
||||
@override_settings(AUTH_USER_MODEL='auth.CustomUserBadRequiredFields')
|
||||
def test_username_not_in_required_fields(self):
|
||||
"USERNAME_FIELD should not appear in REQUIRED_FIELDS."
|
||||
new_io = StringIO()
|
||||
get_validation_errors(new_io, get_app('auth'))
|
||||
get_validation_errors(new_io, app_cache.get_app('auth'))
|
||||
self.assertIn("The field named as the USERNAME_FIELD should not be included in REQUIRED_FIELDS on a swappable User model.", new_io.getvalue())
|
||||
|
||||
@override_settings(AUTH_USER_MODEL='auth.CustomUserNonUniqueUsername')
|
||||
def test_username_non_unique(self):
|
||||
"A non-unique USERNAME_FIELD should raise a model validation error."
|
||||
new_io = StringIO()
|
||||
get_validation_errors(new_io, get_app('auth'))
|
||||
get_validation_errors(new_io, app_cache.get_app('auth'))
|
||||
self.assertIn("The USERNAME_FIELD must be unique. Add unique=True to the field parameters.", new_io.getvalue())
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from django import http
|
||||
from django.apps import app_cache
|
||||
from django.conf import settings
|
||||
from django.contrib import comments
|
||||
from django.contrib.comments import signals
|
||||
@@ -48,7 +49,7 @@ def post_comment(request, next=None, using=None):
|
||||
if ctype is None or object_pk is None:
|
||||
return CommentPostBadRequest("Missing content_type or object_pk field.")
|
||||
try:
|
||||
model = models.get_model(*ctype.split(".", 1))
|
||||
model = app_cache.get_model(*ctype.split(".", 1))
|
||||
target = model._default_manager.using(using).get(pk=object_pk)
|
||||
except TypeError:
|
||||
return CommentPostBadRequest(
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from django.apps import app_cache, UnavailableApp
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db import DEFAULT_DB_ALIAS, router
|
||||
from django.db.models import get_apps, get_model, get_models, signals, UnavailableApp
|
||||
from django.db.models import signals
|
||||
from django.utils.encoding import smart_text
|
||||
from django.utils import six
|
||||
from django.utils.six.moves import input
|
||||
@@ -12,7 +13,7 @@ def update_contenttypes(app, created_models, verbosity=2, db=DEFAULT_DB_ALIAS, *
|
||||
entries that no longer have a matching model class.
|
||||
"""
|
||||
try:
|
||||
get_model('contenttypes', 'ContentType')
|
||||
app_cache.get_model('contenttypes', 'ContentType')
|
||||
except UnavailableApp:
|
||||
return
|
||||
|
||||
@@ -20,7 +21,7 @@ def update_contenttypes(app, created_models, verbosity=2, db=DEFAULT_DB_ALIAS, *
|
||||
return
|
||||
|
||||
ContentType.objects.clear_cache()
|
||||
app_models = get_models(app)
|
||||
app_models = app_cache.get_models(app)
|
||||
if not app_models:
|
||||
return
|
||||
# They all have the same app_label, get the first one.
|
||||
@@ -85,7 +86,7 @@ If you're unsure, answer 'no'.
|
||||
|
||||
|
||||
def update_all_contenttypes(verbosity=2, **kwargs):
|
||||
for app in get_apps():
|
||||
for app in app_cache.get_apps():
|
||||
update_contenttypes(app, None, verbosity, **kwargs)
|
||||
|
||||
signals.post_migrate.connect(update_contenttypes)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from django.apps import app_cache
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils.encoding import smart_text, force_text
|
||||
@@ -156,7 +157,7 @@ class ContentType(models.Model):
|
||||
|
||||
def model_class(self):
|
||||
"Returns the Python model class for this type of content."
|
||||
return models.get_model(self.app_label, self.model,
|
||||
return app_cache.get_model(self.app_label, self.model,
|
||||
only_installed=False)
|
||||
|
||||
def get_object_for_this_type(self, **kwargs):
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from django.apps import app_cache
|
||||
from django.core import urlresolvers
|
||||
from django.contrib.sitemaps import Sitemap
|
||||
from django.contrib.gis.db.models.fields import GeometryField
|
||||
@@ -25,7 +26,7 @@ class KMLSitemap(Sitemap):
|
||||
"""
|
||||
kml_sources = []
|
||||
if sources is None:
|
||||
sources = models.get_models()
|
||||
sources = app_cache.get_models()
|
||||
for source in sources:
|
||||
if isinstance(source, models.base.ModelBase):
|
||||
for field in source._meta.fields:
|
||||
|
||||
@@ -2,6 +2,7 @@ from __future__ import unicode_literals
|
||||
|
||||
import warnings
|
||||
|
||||
from django.apps import app_cache
|
||||
from django.http import HttpResponse, Http404
|
||||
from django.template import loader
|
||||
from django.contrib.sites.models import get_current_site
|
||||
@@ -9,7 +10,6 @@ from django.core import urlresolvers
|
||||
from django.core.paginator import EmptyPage, PageNotAnInteger
|
||||
from django.contrib.gis.db.models.fields import GeometryField
|
||||
from django.db import connections, DEFAULT_DB_ALIAS
|
||||
from django.db.models import get_model
|
||||
from django.db.models.fields import FieldDoesNotExist
|
||||
from django.utils import six
|
||||
from django.utils.translation import ugettext as _
|
||||
@@ -81,7 +81,7 @@ def kml(request, label, model, field_name=None, compress=False, using=DEFAULT_DB
|
||||
must be that of a geographic field.
|
||||
"""
|
||||
placemarks = []
|
||||
klass = get_model(label, model)
|
||||
klass = app_cache.get_model(label, model)
|
||||
if not klass:
|
||||
raise Http404('You must supply a valid app label and module name. Got "%s.%s"' % (label, model))
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.apps import app_cache
|
||||
from django.db import models
|
||||
|
||||
|
||||
@@ -31,7 +32,7 @@ def check_boolean_field_default_value():
|
||||
warns the user that the default has changed from False to Null.
|
||||
"""
|
||||
fields = []
|
||||
for cls in models.get_models():
|
||||
for cls in app_cache.get_models():
|
||||
opts = cls._meta
|
||||
for f in opts.local_fields:
|
||||
if isinstance(f, models.BooleanField) and not f.has_default():
|
||||
|
||||
@@ -342,11 +342,11 @@ class AppCommand(BaseCommand):
|
||||
args = '<appname appname ...>'
|
||||
|
||||
def handle(self, *app_labels, **options):
|
||||
from django.db import models
|
||||
from django.apps import app_cache
|
||||
if not app_labels:
|
||||
raise CommandError('Enter at least one appname.')
|
||||
try:
|
||||
app_list = [models.get_app(app_label) for app_label in app_labels]
|
||||
app_list = [app_cache.get_app(app_label) for app_label in app_labels]
|
||||
except (ImproperlyConfigured, ImportError) as e:
|
||||
raise CommandError("%s. Are you sure your INSTALLED_APPS setting is correct?" % e)
|
||||
output = []
|
||||
|
||||
@@ -38,7 +38,7 @@ class Command(BaseCommand):
|
||||
args = '[appname appname.ModelName ...]'
|
||||
|
||||
def handle(self, *app_labels, **options):
|
||||
from django.db.models import get_app, get_apps, get_model
|
||||
from django.apps import app_cache
|
||||
|
||||
format = options.get('format')
|
||||
indent = options.get('indent')
|
||||
@@ -64,13 +64,13 @@ class Command(BaseCommand):
|
||||
for exclude in excludes:
|
||||
if '.' in exclude:
|
||||
app_label, model_name = exclude.split('.', 1)
|
||||
model_obj = get_model(app_label, model_name)
|
||||
model_obj = app_cache.get_model(app_label, model_name)
|
||||
if not model_obj:
|
||||
raise CommandError('Unknown model in excludes: %s' % exclude)
|
||||
excluded_models.add(model_obj)
|
||||
else:
|
||||
try:
|
||||
app_obj = get_app(exclude)
|
||||
app_obj = app_cache.get_app(exclude)
|
||||
excluded_apps.add(app_obj)
|
||||
except ImproperlyConfigured:
|
||||
raise CommandError('Unknown app in excludes: %s' % exclude)
|
||||
@@ -78,7 +78,7 @@ class Command(BaseCommand):
|
||||
if len(app_labels) == 0:
|
||||
if primary_keys:
|
||||
raise CommandError("You can only use --pks option with one model")
|
||||
app_list = OrderedDict((app, None) for app in get_apps() if app not in excluded_apps)
|
||||
app_list = OrderedDict((app, None) for app in app_cache.get_apps() if app not in excluded_apps)
|
||||
else:
|
||||
if len(app_labels) > 1 and primary_keys:
|
||||
raise CommandError("You can only use --pks option with one model")
|
||||
@@ -87,12 +87,12 @@ class Command(BaseCommand):
|
||||
try:
|
||||
app_label, model_label = label.split('.')
|
||||
try:
|
||||
app = get_app(app_label)
|
||||
app = app_cache.get_app(app_label)
|
||||
except ImproperlyConfigured:
|
||||
raise CommandError("Unknown application: %s" % app_label)
|
||||
if app in excluded_apps:
|
||||
continue
|
||||
model = get_model(app_label, model_label)
|
||||
model = app_cache.get_model(app_label, model_label)
|
||||
if model is None:
|
||||
raise CommandError("Unknown model: %s.%s" % (app_label, model_label))
|
||||
|
||||
@@ -107,7 +107,7 @@ class Command(BaseCommand):
|
||||
# This is just an app - no model qualifier
|
||||
app_label = label
|
||||
try:
|
||||
app = get_app(app_label)
|
||||
app = app_cache.get_app(app_label)
|
||||
except ImproperlyConfigured:
|
||||
raise CommandError("Unknown application: %s" % app_label)
|
||||
if app in excluded_apps:
|
||||
@@ -160,13 +160,13 @@ def sort_dependencies(app_list):
|
||||
is serialized before a normal model, and any model with a natural key
|
||||
dependency has it's dependencies serialized first.
|
||||
"""
|
||||
from django.db.models import get_model, get_models
|
||||
from django.apps import app_cache
|
||||
# Process the list of models, and get the list of dependencies
|
||||
model_dependencies = []
|
||||
models = set()
|
||||
for app, model_list in app_list:
|
||||
if model_list is None:
|
||||
model_list = get_models(app)
|
||||
model_list = app_cache.get_models(app)
|
||||
|
||||
for model in model_list:
|
||||
models.add(model)
|
||||
@@ -174,7 +174,7 @@ def sort_dependencies(app_list):
|
||||
if hasattr(model, 'natural_key'):
|
||||
deps = getattr(model.natural_key, 'dependencies', [])
|
||||
if deps:
|
||||
deps = [get_model(*d.split('.')) for d in deps]
|
||||
deps = [app_cache.get_model(*d.split('.')) for d in deps]
|
||||
else:
|
||||
deps = []
|
||||
|
||||
|
||||
@@ -2,8 +2,9 @@ import sys
|
||||
from importlib import import_module
|
||||
from optparse import make_option
|
||||
|
||||
from django.apps import app_cache
|
||||
from django.conf import settings
|
||||
from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS
|
||||
from django.db import connections, router, transaction, DEFAULT_DB_ALIAS
|
||||
from django.core.management import call_command
|
||||
from django.core.management.base import NoArgsCommand, CommandError
|
||||
from django.core.management.color import no_style
|
||||
@@ -93,6 +94,6 @@ Are you sure you want to do this?
|
||||
# Emit the post migrate signal. This allows individual applications to
|
||||
# respond as if the database had been migrated from scratch.
|
||||
all_models = []
|
||||
for app in models.get_apps():
|
||||
for app in app_cache.get_apps():
|
||||
all_models.extend(router.get_migratable_models(app, database, include_auto_created=True))
|
||||
emit_post_migrate_signal(set(all_models), verbosity, interactive, database)
|
||||
|
||||
@@ -7,13 +7,13 @@ import warnings
|
||||
import zipfile
|
||||
from optparse import make_option
|
||||
|
||||
from django.apps import app_cache
|
||||
from django.conf import settings
|
||||
from django.core import serializers
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.core.management.color import no_style
|
||||
from django.db import (connections, router, transaction, DEFAULT_DB_ALIAS,
|
||||
IntegrityError, DatabaseError)
|
||||
from django.db.models import get_app_paths
|
||||
from django.utils import lru_cache
|
||||
from django.utils.encoding import force_text
|
||||
from django.utils.functional import cached_property
|
||||
@@ -230,7 +230,7 @@ class Command(BaseCommand):
|
||||
current directory.
|
||||
"""
|
||||
dirs = []
|
||||
for path in get_app_paths():
|
||||
for path in app_cache.get_app_paths():
|
||||
d = os.path.join(path, 'fixtures')
|
||||
if os.path.isdir(d):
|
||||
dirs.append(d)
|
||||
|
||||
@@ -3,7 +3,7 @@ import os
|
||||
import operator
|
||||
from optparse import make_option
|
||||
|
||||
from django.apps.cache import cache
|
||||
from django.apps import app_cache
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.db import connections, DEFAULT_DB_ALIAS, migrations
|
||||
@@ -38,7 +38,7 @@ class Command(BaseCommand):
|
||||
bad_app_labels = set()
|
||||
for app_label in app_labels:
|
||||
try:
|
||||
cache.get_app(app_label)
|
||||
app_cache.get_app(app_label)
|
||||
except ImproperlyConfigured:
|
||||
bad_app_labels.add(app_label)
|
||||
if bad_app_labels:
|
||||
@@ -73,7 +73,7 @@ class Command(BaseCommand):
|
||||
# Detect changes
|
||||
autodetector = MigrationAutodetector(
|
||||
loader.graph.project_state(),
|
||||
ProjectState.from_app_cache(cache),
|
||||
ProjectState.from_app_cache(app_cache),
|
||||
InteractiveMigrationQuestioner(specified_apps=app_labels),
|
||||
)
|
||||
changes = autodetector.changes(graph=loader.graph, trim_to_apps=app_labels or None)
|
||||
|
||||
@@ -6,13 +6,13 @@ from importlib import import_module
|
||||
import itertools
|
||||
import traceback
|
||||
|
||||
from django.apps.cache import cache
|
||||
from django.apps import app_cache
|
||||
from django.conf import settings
|
||||
from django.core.management import call_command
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.core.management.color import no_style
|
||||
from django.core.management.sql import custom_sql_for_model, emit_post_migrate_signal, emit_pre_migrate_signal
|
||||
from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS
|
||||
from django.db import connections, router, transaction, DEFAULT_DB_ALIAS
|
||||
from django.db.migrations.executor import MigrationExecutor
|
||||
from django.db.migrations.loader import MigrationLoader, AmbiguityError
|
||||
from django.db.migrations.state import ProjectState
|
||||
@@ -136,7 +136,7 @@ class Command(BaseCommand):
|
||||
# If there's changes that aren't in migrations yet, tell them how to fix it.
|
||||
autodetector = MigrationAutodetector(
|
||||
executor.loader.graph.project_state(),
|
||||
ProjectState.from_app_cache(cache),
|
||||
ProjectState.from_app_cache(app_cache),
|
||||
)
|
||||
changes = autodetector.changes(graph=executor.loader.graph)
|
||||
if changes:
|
||||
@@ -182,7 +182,7 @@ class Command(BaseCommand):
|
||||
all_models = [
|
||||
(app.__name__.split('.')[-2],
|
||||
router.get_migratable_models(app, connection.alias, include_auto_created=True))
|
||||
for app in models.get_apps() if app.__name__.split('.')[-2] in apps
|
||||
for app in app_cache.get_apps() if app.__name__.split('.')[-2] in apps
|
||||
]
|
||||
|
||||
def model_installed(model):
|
||||
|
||||
@@ -66,8 +66,8 @@ class Command(NoArgsCommand):
|
||||
def handle_noargs(self, **options):
|
||||
# XXX: (Temporary) workaround for ticket #1796: force early loading of all
|
||||
# models from installed apps.
|
||||
from django.apps.cache import get_models
|
||||
get_models()
|
||||
from django.apps import app_cache
|
||||
app_cache.get_models()
|
||||
|
||||
use_plain = options.get('plain', False)
|
||||
no_startup = options.get('no_startup', False)
|
||||
|
||||
@@ -2,8 +2,9 @@ from __future__ import unicode_literals
|
||||
|
||||
from optparse import make_option
|
||||
|
||||
from django.apps import app_cache
|
||||
from django.core.management.base import AppCommand
|
||||
from django.db import connections, models, DEFAULT_DB_ALIAS
|
||||
from django.db import connections, DEFAULT_DB_ALIAS
|
||||
|
||||
|
||||
class Command(AppCommand):
|
||||
@@ -20,4 +21,4 @@ class Command(AppCommand):
|
||||
|
||||
def handle_app(self, app, **options):
|
||||
connection = connections[options.get('database')]
|
||||
return '\n'.join(connection.ops.sequence_reset_sql(self.style, models.get_models(app, include_auto_created=True)))
|
||||
return '\n'.join(connection.ops.sequence_reset_sql(self.style, app_cache.get_models(app, include_auto_created=True)))
|
||||
|
||||
@@ -5,6 +5,7 @@ import os
|
||||
import re
|
||||
import warnings
|
||||
|
||||
from django.apps import app_cache
|
||||
from django.conf import settings
|
||||
from django.core.management.base import CommandError
|
||||
from django.db import models, router
|
||||
@@ -24,7 +25,7 @@ def sql_create(app, style, connection):
|
||||
# We trim models from the current app so that the sqlreset command does not
|
||||
# generate invalid SQL (leaving models out of known_models is harmless, so
|
||||
# we can be conservative).
|
||||
app_models = models.get_models(app, include_auto_created=True)
|
||||
app_models = app_cache.get_models(app, include_auto_created=True)
|
||||
final_output = []
|
||||
tables = connection.introspection.table_names()
|
||||
known_models = set(model for model in connection.introspection.installed_models(tables) if model not in app_models)
|
||||
@@ -168,7 +169,7 @@ def _split_statements(content):
|
||||
def custom_sql_for_model(model, style, connection):
|
||||
opts = model._meta
|
||||
app_dirs = []
|
||||
app_dir = models.get_app_path(model._meta.app_label)
|
||||
app_dir = app_cache.get_app_path(model._meta.app_label)
|
||||
app_dirs.append(os.path.normpath(os.path.join(app_dir, 'sql')))
|
||||
|
||||
# Deprecated location -- remove in Django 1.9
|
||||
@@ -206,7 +207,7 @@ def custom_sql_for_model(model, style, connection):
|
||||
|
||||
def emit_pre_migrate_signal(create_models, verbosity, interactive, db):
|
||||
# Emit the pre_migrate signal for every application.
|
||||
for app in models.get_apps():
|
||||
for app in app_cache.get_apps():
|
||||
app_name = app.__name__.split('.')[-2]
|
||||
if verbosity >= 2:
|
||||
print("Running pre-migrate handlers for application %s" % app_name)
|
||||
@@ -219,7 +220,7 @@ def emit_pre_migrate_signal(create_models, verbosity, interactive, db):
|
||||
|
||||
def emit_post_migrate_signal(created_models, verbosity, interactive, db):
|
||||
# Emit the post_migrate signal for every application.
|
||||
for app in models.get_apps():
|
||||
for app in app_cache.get_apps():
|
||||
app_name = app.__name__.split('.')[-2]
|
||||
if verbosity >= 2:
|
||||
print("Running post-migrate handlers for application %s" % app_name)
|
||||
|
||||
@@ -26,16 +26,16 @@ def get_validation_errors(outfile, app=None):
|
||||
validates all models of all installed apps. Writes errors, if any, to outfile.
|
||||
Returns number of errors.
|
||||
"""
|
||||
from django.apps.cache import get_app_errors
|
||||
from django.apps import app_cache
|
||||
from django.db import connection, models
|
||||
from django.db.models.deletion import SET_NULL, SET_DEFAULT
|
||||
|
||||
e = ModelErrorCollection(outfile)
|
||||
|
||||
for (app_name, error) in get_app_errors().items():
|
||||
for (app_name, error) in app_cache.get_app_errors().items():
|
||||
e.add(app_name, error)
|
||||
|
||||
for cls in models.get_models(app, include_swapped=True):
|
||||
for cls in app_cache.get_models(app, include_swapped=True):
|
||||
opts = cls._meta
|
||||
|
||||
# Check swappable attribute.
|
||||
@@ -45,7 +45,7 @@ def get_validation_errors(outfile, app=None):
|
||||
except ValueError:
|
||||
e.add(opts, "%s is not of the form 'app_label.app_name'." % opts.swappable)
|
||||
continue
|
||||
if not models.get_model(app_label, model_name):
|
||||
if not app_cache.get_model(app_label, model_name):
|
||||
e.add(opts, "Model has been swapped out for '%s' which has not been installed or is abstract." % opts.swapped)
|
||||
# No need to perform any other validation checks on a swapped model.
|
||||
continue
|
||||
@@ -155,7 +155,7 @@ def get_validation_errors(outfile, app=None):
|
||||
# Check to see if the related field will clash with any existing
|
||||
# fields, m2m fields, m2m related objects or related objects
|
||||
if f.rel:
|
||||
if f.rel.to not in models.get_models():
|
||||
if f.rel.to not in app_cache.get_models():
|
||||
# If the related model is swapped, provide a hint;
|
||||
# otherwise, the model just hasn't been installed.
|
||||
if not isinstance(f.rel.to, six.string_types) and f.rel.to._meta.swapped:
|
||||
@@ -210,7 +210,7 @@ def get_validation_errors(outfile, app=None):
|
||||
# Check to see if the related m2m field will clash with any
|
||||
# existing fields, m2m fields, m2m related objects or related
|
||||
# objects
|
||||
if f.rel.to not in models.get_models():
|
||||
if f.rel.to not in app_cache.get_models():
|
||||
# If the related model is swapped, provide a hint;
|
||||
# otherwise, the model just hasn't been installed.
|
||||
if not isinstance(f.rel.to, six.string_types) and f.rel.to._meta.swapped:
|
||||
@@ -268,7 +268,7 @@ def get_validation_errors(outfile, app=None):
|
||||
)
|
||||
else:
|
||||
seen_to = True
|
||||
if f.rel.through not in models.get_models(include_auto_created=True):
|
||||
if f.rel.through not in app_cache.get_models(include_auto_created=True):
|
||||
e.add(opts, "'%s' specifies an m2m relation through model "
|
||||
"%s, which has not been installed." % (f.name, f.rel.through))
|
||||
signature = (f.rel.to, cls, f.rel.through)
|
||||
|
||||
@@ -3,6 +3,7 @@ Module for abstract serializer/unserializer base classes.
|
||||
"""
|
||||
import warnings
|
||||
|
||||
from django.apps import app_cache
|
||||
from django.db import models
|
||||
from django.utils import six
|
||||
|
||||
@@ -139,7 +140,7 @@ class Deserializer(six.Iterator):
|
||||
# hack to make sure that the models have all been loaded before
|
||||
# deserialization starts (otherwise subclass calls to get_model()
|
||||
# and friends might fail...)
|
||||
models.get_apps()
|
||||
app_cache.get_apps()
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
@@ -5,6 +5,7 @@ other serializers.
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.apps import app_cache
|
||||
from django.conf import settings
|
||||
from django.core.serializers import base
|
||||
from django.db import models, DEFAULT_DB_ALIAS
|
||||
@@ -87,7 +88,7 @@ def Deserializer(object_list, **options):
|
||||
db = options.pop('using', DEFAULT_DB_ALIAS)
|
||||
ignore = options.pop('ignorenonexistent', False)
|
||||
|
||||
models.get_apps()
|
||||
app_cache.get_apps()
|
||||
for d in object_list:
|
||||
# Look up the model and starting build a dict of data for it.
|
||||
Model = _get_model(d["model"])
|
||||
@@ -153,7 +154,7 @@ def _get_model(model_identifier):
|
||||
Helper to look up a model from an "app_label.model_name" string.
|
||||
"""
|
||||
try:
|
||||
Model = models.get_model(*model_identifier.split("."))
|
||||
Model = app_cache.get_model(*model_identifier.split("."))
|
||||
except TypeError:
|
||||
Model = None
|
||||
if Model is None:
|
||||
|
||||
@@ -4,6 +4,7 @@ XML serializer.
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.apps import app_cache
|
||||
from django.conf import settings
|
||||
from django.core.serializers import base
|
||||
from django.db import models, DEFAULT_DB_ALIAS
|
||||
@@ -276,7 +277,7 @@ class Deserializer(base.Deserializer):
|
||||
"<%s> node is missing the required '%s' attribute"
|
||||
% (node.nodeName, attr))
|
||||
try:
|
||||
Model = models.get_model(*model_identifier.split("."))
|
||||
Model = app_cache.get_model(*model_identifier.split("."))
|
||||
except TypeError:
|
||||
Model = None
|
||||
if Model is None:
|
||||
|
||||
@@ -1268,9 +1268,10 @@ class BaseDatabaseIntrospection(object):
|
||||
If only_existing is True, the resulting list will only include the tables
|
||||
that actually exist in the database.
|
||||
"""
|
||||
from django.db import models, router
|
||||
from django.apps import app_cache
|
||||
from django.db import router
|
||||
tables = set()
|
||||
for app in models.get_apps():
|
||||
for app in app_cache.get_apps():
|
||||
for model in router.get_migratable_models(app, self.connection.alias):
|
||||
if not model._meta.managed:
|
||||
continue
|
||||
@@ -1288,9 +1289,10 @@ class BaseDatabaseIntrospection(object):
|
||||
|
||||
def installed_models(self, tables):
|
||||
"Returns a set of all models represented by the provided list of table names."
|
||||
from django.db import models, router
|
||||
from django.apps import app_cache
|
||||
from django.db import router
|
||||
all_models = []
|
||||
for app in models.get_apps():
|
||||
for app in app_cache.get_apps():
|
||||
all_models.extend(router.get_migratable_models(app, self.connection.alias))
|
||||
tables = list(map(self.table_name_converter, tables))
|
||||
return set([
|
||||
@@ -1300,9 +1302,10 @@ class BaseDatabaseIntrospection(object):
|
||||
|
||||
def sequence_list(self):
|
||||
"Returns a list of information about all DB sequences for all models in all apps."
|
||||
from django.apps import app_cache
|
||||
from django.db import models, router
|
||||
|
||||
apps = models.get_apps()
|
||||
apps = app_cache.get_apps()
|
||||
sequence_list = []
|
||||
|
||||
for app in apps:
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
from importlib import import_module
|
||||
import os
|
||||
import sys
|
||||
from importlib import import_module
|
||||
from django.apps.cache import cache
|
||||
|
||||
from django.apps import app_cache
|
||||
from django.db.migrations.recorder import MigrationRecorder
|
||||
from django.db.migrations.graph import MigrationGraph
|
||||
from django.utils import six
|
||||
@@ -45,7 +46,7 @@ class MigrationLoader(object):
|
||||
if app_label in settings.MIGRATION_MODULES:
|
||||
return settings.MIGRATION_MODULES[app_label]
|
||||
else:
|
||||
return '%s.migrations' % cache.get_app_package(app_label)
|
||||
return '%s.migrations' % app_cache.get_app_package(app_label)
|
||||
|
||||
def load_disk(self):
|
||||
"""
|
||||
@@ -54,7 +55,7 @@ class MigrationLoader(object):
|
||||
self.disk_migrations = {}
|
||||
self.unmigrated_apps = set()
|
||||
self.migrated_apps = set()
|
||||
for app in cache.get_apps():
|
||||
for app in app_cache.get_apps():
|
||||
# Get the migrations module directory
|
||||
app_label = app.__name__.split(".")[-2]
|
||||
module_name = self.migrations_module(app_label)
|
||||
|
||||
@@ -2,7 +2,7 @@ import importlib
|
||||
import os
|
||||
import sys
|
||||
|
||||
from django.apps.cache import cache
|
||||
from django.apps import app_cache
|
||||
from django.utils import datetime_safe
|
||||
from django.utils.six.moves import input
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
@@ -29,7 +29,7 @@ class MigrationQuestioner(object):
|
||||
# Apps from the new app template will have these; the python
|
||||
# file check will ensure we skip South ones.
|
||||
try:
|
||||
models_module = cache.get_app(app_label)
|
||||
models_module = app_cache.get_app(app_label)
|
||||
except ImproperlyConfigured: # It's a fake app
|
||||
return self.defaults.get("ask_initial", False)
|
||||
migrations_import_path = "%s.migrations" % models_module.__package__
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import datetime
|
||||
import types
|
||||
import os
|
||||
from importlib import import_module
|
||||
from django.apps.cache import cache
|
||||
import os
|
||||
import types
|
||||
|
||||
from django.apps import app_cache
|
||||
from django.db import models
|
||||
from django.db.migrations.loader import MigrationLoader
|
||||
from django.utils.encoding import force_text
|
||||
@@ -67,9 +69,9 @@ class MigrationWriter(object):
|
||||
migrations_module = import_module(migrations_package_name)
|
||||
basedir = os.path.dirname(migrations_module.__file__)
|
||||
except ImportError:
|
||||
app = cache.get_app(self.migration.app_label)
|
||||
app_path = cache._get_app_path(app)
|
||||
app_package_name = cache._get_app_package(app)
|
||||
app = app_cache.get_app(self.migration.app_label)
|
||||
app_path = app_cache._get_app_path(app)
|
||||
app_package_name = app_cache._get_app_package(app)
|
||||
migrations_package_basename = migrations_package_name.split(".")[-1]
|
||||
|
||||
# Alright, see if it's a direct submodule of the app
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
from functools import wraps
|
||||
|
||||
from django.apps.cache import ( # NOQA
|
||||
get_apps, get_app_path, get_app_paths, get_app, get_models, get_model,
|
||||
register_models, UnavailableApp)
|
||||
from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured # NOQA
|
||||
from django.db.models.query import Q, QuerySet, Prefetch # NOQA
|
||||
from django.db.models.expressions import F # NOQA
|
||||
|
||||
@@ -5,7 +5,8 @@ import sys
|
||||
from functools import update_wrapper
|
||||
from django.utils.six.moves import zip
|
||||
|
||||
from django.apps.cache import get_model, MODELS_MODULE_NAME
|
||||
from django.apps import app_cache
|
||||
from django.apps.cache import MODELS_MODULE_NAME
|
||||
import django.db.models.manager # NOQA: Imported to register signal handler.
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import (ObjectDoesNotExist,
|
||||
@@ -1066,7 +1067,7 @@ def model_unpickle(model_id, attrs, factory):
|
||||
Used to unpickle Model subclasses with deferred fields.
|
||||
"""
|
||||
if isinstance(model_id, tuple):
|
||||
model = get_model(*model_id)
|
||||
model = app_cache.get_model(*model_id)
|
||||
else:
|
||||
# Backwards compat - the model was cached directly in earlier versions.
|
||||
model = model_id
|
||||
|
||||
@@ -9,7 +9,7 @@ import warnings
|
||||
from base64 import b64decode, b64encode
|
||||
from itertools import tee
|
||||
|
||||
from django.apps.cache import get_model
|
||||
from django.apps import app_cache
|
||||
from django.db import connection
|
||||
from django.db.models.query_utils import QueryWrapper
|
||||
from django.conf import settings
|
||||
@@ -51,7 +51,7 @@ BLANK_CHOICE_DASH = [("", "---------")]
|
||||
|
||||
|
||||
def _load_field(app_label, model_name, field_name):
|
||||
return get_model(app_label, model_name)._meta.get_field_by_name(field_name)[0]
|
||||
return app_cache.get_model(app_label, model_name)._meta.get_field_by_name(field_name)[0]
|
||||
|
||||
|
||||
class FieldDoesNotExist(Exception):
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import warnings
|
||||
|
||||
from django.apps.cache import cache
|
||||
from django.apps import app_cache
|
||||
|
||||
warnings.warn(
|
||||
"The utilities in django.db.models.loading are deprecated "
|
||||
@@ -12,14 +12,14 @@ __all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models',
|
||||
|
||||
# These methods were always module level, so are kept that way for backwards
|
||||
# compatibility.
|
||||
get_apps = cache.get_apps
|
||||
get_app_package = cache.get_app_package
|
||||
get_app_path = cache.get_app_path
|
||||
get_app_paths = cache.get_app_paths
|
||||
get_app = cache.get_app
|
||||
get_app_errors = cache.get_app_errors
|
||||
get_models = cache.get_models
|
||||
get_model = cache.get_model
|
||||
register_models = cache.register_models
|
||||
load_app = cache.load_app
|
||||
app_cache_ready = cache.app_cache_ready
|
||||
get_apps = app_cache.get_apps
|
||||
get_app_package = app_cache.get_app_package
|
||||
get_app_path = app_cache.get_app_path
|
||||
get_app_paths = app_cache.get_app_paths
|
||||
get_app = app_cache.get_app
|
||||
get_app_errors = app_cache.get_app_errors
|
||||
get_models = app_cache.get_models
|
||||
get_model = app_cache.get_model
|
||||
register_models = app_cache.register_models
|
||||
load_app = app_cache.load_app
|
||||
app_cache_ready = app_cache.app_cache_ready
|
||||
|
||||
@@ -5,7 +5,7 @@ import re
|
||||
from bisect import bisect
|
||||
import warnings
|
||||
|
||||
from django.apps.cache import app_cache_ready, cache
|
||||
from django.apps import app_cache
|
||||
from django.conf import settings
|
||||
from django.db.models.fields.related import ManyToManyRel
|
||||
from django.db.models.fields import AutoField, FieldDoesNotExist
|
||||
@@ -89,7 +89,7 @@ class Options(object):
|
||||
self.related_fkey_lookups = []
|
||||
|
||||
# A custom AppCache to use, if you're making a separate model set.
|
||||
self.app_cache = cache
|
||||
self.app_cache = app_cache
|
||||
|
||||
def contribute_to_class(self, cls, name):
|
||||
from django.db import connection
|
||||
@@ -432,7 +432,7 @@ class Options(object):
|
||||
if hasattr(f, 'related'):
|
||||
cache[f.name] = cache[f.attname] = (
|
||||
f.related, None if f.model == self.model else f.model, True, False)
|
||||
if app_cache_ready():
|
||||
if app_cache.app_cache_ready():
|
||||
self._name_map = cache
|
||||
return cache
|
||||
|
||||
@@ -558,7 +558,7 @@ class Options(object):
|
||||
and not isinstance(f.rel.to, six.string_types)
|
||||
and self == f.rel.to._meta):
|
||||
cache[f.related] = None
|
||||
if app_cache_ready():
|
||||
if app_cache.app_cache_ready():
|
||||
self._related_many_to_many_cache = cache
|
||||
return cache
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from collections import defaultdict
|
||||
|
||||
from django.apps.cache import get_model
|
||||
from django.apps import app_cache
|
||||
from django.dispatch import Signal
|
||||
from django.utils import six
|
||||
|
||||
@@ -41,7 +41,7 @@ class ModelSignal(Signal):
|
||||
"Specified sender must either be a model or a "
|
||||
"model name of the 'app_label.ModelName' form."
|
||||
)
|
||||
sender = get_model(app_label, object_name, only_installed=False)
|
||||
sender = app_cache.get_model(app_label, object_name, only_installed=False)
|
||||
if sender is None:
|
||||
reference = (app_label, object_name)
|
||||
self.unresolved_references[reference].append(
|
||||
|
||||
@@ -282,6 +282,6 @@ class ConnectionRouter(object):
|
||||
"""
|
||||
Return app models allowed to be synchronized on provided db.
|
||||
"""
|
||||
from .models import get_models
|
||||
return [model for model in get_models(app, include_auto_created=include_auto_created)
|
||||
from django.apps import app_cache
|
||||
return [model for model in app_cache.get_models(app, include_auto_created=include_auto_created)
|
||||
if self.allow_migrate(db, model)]
|
||||
|
||||
@@ -9,7 +9,7 @@ import re
|
||||
import unittest as real_unittest
|
||||
import warnings
|
||||
|
||||
from django.db.models import get_app, get_apps
|
||||
from django.apps import app_cache
|
||||
from django.test import _doctest as doctest
|
||||
from django.test import runner
|
||||
from django.test.utils import compare_xml, strip_quotes
|
||||
@@ -179,7 +179,7 @@ def build_test(label):
|
||||
#
|
||||
# First, look for TestCase instances with a name that matches
|
||||
#
|
||||
app_module = get_app(parts[0])
|
||||
app_module = app_cache.get_app(parts[0])
|
||||
test_module = get_tests(app_module)
|
||||
TestClass = getattr(app_module, parts[1], None)
|
||||
|
||||
@@ -241,10 +241,10 @@ class DjangoTestSuiteRunner(runner.DiscoverRunner):
|
||||
if '.' in label:
|
||||
suite.addTest(build_test(label))
|
||||
else:
|
||||
app = get_app(label)
|
||||
app = app_cache.get_app(label)
|
||||
suite.addTest(build_suite(app))
|
||||
else:
|
||||
for app in get_apps():
|
||||
for app in app_cache.get_apps():
|
||||
suite.addTest(build_suite(app))
|
||||
|
||||
if extra_tests:
|
||||
|
||||
@@ -15,7 +15,7 @@ import unittest
|
||||
from unittest import skipIf # NOQA: Imported here for backward compatibility
|
||||
from unittest.util import safe_repr
|
||||
|
||||
from django.apps.cache import cache
|
||||
from django.apps import app_cache
|
||||
from django.conf import settings
|
||||
from django.core import mail
|
||||
from django.core.exceptions import ValidationError, ImproperlyConfigured
|
||||
@@ -725,14 +725,14 @@ class TransactionTestCase(SimpleTestCase):
|
||||
"""
|
||||
super(TransactionTestCase, self)._pre_setup()
|
||||
if self.available_apps is not None:
|
||||
cache.set_available_apps(self.available_apps)
|
||||
app_cache.set_available_apps(self.available_apps)
|
||||
for db_name in self._databases_names(include_mirrors=False):
|
||||
flush.Command.emit_post_migrate(verbosity=0, interactive=False, database=db_name)
|
||||
try:
|
||||
self._fixture_setup()
|
||||
except Exception:
|
||||
if self.available_apps is not None:
|
||||
cache.unset_available_apps()
|
||||
app_cache.unset_available_apps()
|
||||
raise
|
||||
|
||||
def _databases_names(self, include_mirrors=True):
|
||||
@@ -786,7 +786,7 @@ class TransactionTestCase(SimpleTestCase):
|
||||
for conn in connections.all():
|
||||
conn.close()
|
||||
finally:
|
||||
cache.unset_available_apps()
|
||||
app_cache.unset_available_apps()
|
||||
|
||||
def _fixture_teardown(self):
|
||||
# Allow TRUNCATE ... CASCADE and don't emit the post_migrate signal
|
||||
|
||||
Reference in New Issue
Block a user