From 7662520667fc822ba84db9f821ba7319b4f060fa Mon Sep 17 00:00:00 2001 From: Luke Plant Date: Tue, 7 Feb 2006 22:40:11 +0000 Subject: [PATCH] magic-removal: Changed registering of models so that only one 'model name' can be registered per app_label (fixes duplicates in the admin app list if the models are in separate files) git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2290 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/loading.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/django/db/models/loading.py b/django/db/models/loading.py index a64c3ee0fb..babc7285d2 100644 --- a/django/db/models/loading.py +++ b/django/db/models/loading.py @@ -6,7 +6,8 @@ from django.core.exceptions import ImproperlyConfigured __all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models') _app_list = None # Cache of installed apps. -_app_models = {} # Dictionary of models against app module name +_app_models = {} # Dictionary of models against app label + # Each value is a dictionary of model name: model class def get_apps(): "Returns a list of all installed modules that contain models." @@ -34,7 +35,7 @@ def get_models(app_mod=None): returns a list of all installed models. """ if app_mod: - return _app_models.get(app_mod.__name__.split('.')[-2], ()) + return _app_models.get(app_mod.__name__.split('.')[-2], ()).values() else: model_list = [] for app_mod in get_apps(): @@ -46,14 +47,25 @@ def get_model(app_label, model_name): Returns the model matching the given app_label and case-insensitive model_name. Returns None if no model is found. """ - for app_mod in get_apps(): - for model in get_models(app_mod): - if model._meta.object_name.lower() == model_name and \ - model._meta.app_label == app_label: - return model + get_apps() # initialise + + try: + model_dict = _app_models[app_label] + except KeyError: + return None + + try: + return model_dict[model_name.lower()] + except KeyError: + return None def register_models(app_label, *models): """ Register a set of models as belonging to an app. """ - _app_models.setdefault(app_label, []).extend(models) + for model in models: + # Store as 'name: model' pair in a dictionary + # in the _app_models dictionary + model_name = model._meta.object_name.lower() + model_dict = _app_models.setdefault(app_label, {}) + model_dict[model_name] = model