1
0
mirror of https://github.com/django/django.git synced 2025-06-05 11:39:13 +00:00

Simplified register_models.

Since it's never called with more than one model at a time the current
signature is needlessly complicated.
This commit is contained in:
Aymeric Augustin 2013-12-13 21:29:30 +01:00
parent e85932b54e
commit ebda5800ae
4 changed files with 35 additions and 30 deletions

View File

@ -26,7 +26,7 @@ class AppConfig(object):
self.models_module = models_module self.models_module = models_module
# Mapping of lower case model names to model classes. # Mapping of lower case model names to model classes.
# Populated by AppCache.register_models(). # Populated by calls to AppCache.register_model().
self.models = OrderedDict() self.models = OrderedDict()
# Whether the app is in INSTALLED_APPS or was automatically created # Whether the app is in INSTALLED_APPS or was automatically created

View File

@ -302,32 +302,27 @@ class BaseAppCache(object):
except KeyError: except KeyError:
return None return None
def register_models(self, app_label, *models): def register_model(self, app_label, model):
""" try:
Register a set of models as belonging to an app. app_config = self.app_configs[app_label]
""" except KeyError:
if models: app_config = AppConfig._stub(app_label)
try: self.app_configs[app_label] = app_config
app_config = self.app_configs[app_label] # Add the model to the app_config's models dictionary.
except KeyError: model_name = model._meta.model_name
app_config = AppConfig._stub(app_label) model_dict = app_config.models
self.app_configs[app_label] = app_config if model_name in model_dict:
for model in models: # The same model may be imported via different paths (e.g.
# Add the model to the app_config's models dictionary. # appname.models and project.appname.models). We use the source
model_name = model._meta.model_name # filename as a means to detect identity.
model_dict = app_config.models fname1 = os.path.abspath(upath(sys.modules[model.__module__].__file__))
if model_name in model_dict: fname2 = os.path.abspath(upath(sys.modules[model_dict[model_name].__module__].__file__))
# The same model may be imported via different paths (e.g. # Since the filename extension could be .py the first time and
# appname.models and project.appname.models). We use the source # .pyc or .pyo the second time, ignore the extension when
# filename as a means to detect identity. # comparing.
fname1 = os.path.abspath(upath(sys.modules[model.__module__].__file__)) if os.path.splitext(fname1)[0] == os.path.splitext(fname2)[0]:
fname2 = os.path.abspath(upath(sys.modules[model_dict[model_name].__module__].__file__)) return
# Since the filename extension could be .py the first time and model_dict[model_name] = model
# .pyc or .pyo the second time, ignore the extension when
# comparing.
if os.path.splitext(fname1)[0] == os.path.splitext(fname2)[0]:
continue
model_dict[model_name] = model
self._get_models_cache.clear() self._get_models_cache.clear()
def set_available_apps(self, available): def set_available_apps(self, available):
@ -383,6 +378,16 @@ class BaseAppCache(object):
app_paths.append(self._get_app_path(app)) app_paths.append(self._get_app_path(app))
return app_paths return app_paths
def register_models(self, app_label, *models):
"""
Register a set of models as belonging to an app.
"""
warnings.warn(
"register_models(app_label, models) is deprecated.",
PendingDeprecationWarning, stacklevel=2)
for model in models:
self.register_model(app_label, model)
class AppCache(BaseAppCache): class AppCache(BaseAppCache):
""" """

View File

@ -274,7 +274,7 @@ class ModelBase(type):
new_class._prepare() new_class._prepare()
new_class._meta.app_cache.register_models(new_class._meta.app_label, new_class) new_class._meta.app_cache.register_model(new_class._meta.app_label, new_class)
# Because of the way imports happen (recursively), we may or may not be # Because of the way imports happen (recursively), we may or may not be
# the first time this model tries to register with the framework. There # the first time this model tries to register with the framework. There
# should only be one class for each model, so we always return the # should only be one class for each model, so we always return the

View File

@ -151,7 +151,7 @@ class MakeMigrationsTests(MigrationTestBase):
def test_files_content(self): def test_files_content(self):
self.assertTableNotExists("migrations_unicodemodel") self.assertTableNotExists("migrations_unicodemodel")
app_cache.register_models('migrations', UnicodeModel) app_cache.register_model('migrations', UnicodeModel)
with override_settings(MIGRATION_MODULES={"migrations": self.migration_pkg}): with override_settings(MIGRATION_MODULES={"migrations": self.migration_pkg}):
call_command("makemigrations", "migrations", verbosity=0) call_command("makemigrations", "migrations", verbosity=0)
@ -187,7 +187,7 @@ class MakeMigrationsTests(MigrationTestBase):
def test_failing_migration(self): def test_failing_migration(self):
#21280 - If a migration fails to serialize, it shouldn't generate an empty file. #21280 - If a migration fails to serialize, it shouldn't generate an empty file.
app_cache.register_models('migrations', UnserializableModel) app_cache.register_model('migrations', UnserializableModel)
with six.assertRaisesRegex(self, ValueError, r'Cannot serialize'): with six.assertRaisesRegex(self, ValueError, r'Cannot serialize'):
with override_settings(MIGRATION_MODULES={"migrations": self.migration_pkg}): with override_settings(MIGRATION_MODULES={"migrations": self.migration_pkg}):