diff options
| author | Tim Graham <timograham@gmail.com> | 2013-07-16 13:21:17 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2013-07-16 19:32:30 -0400 |
| commit | 2333c9662b5ffbaec5a10fa2973b4e6e7b0555bd (patch) | |
| tree | 672bc21c469c70182021b0872eb40d76377b07cb /django/db | |
| parent | fba6c2ede7b25ed8c71a18dede00865dbc5b85af (diff) | |
Fixed #14007 -- Added model discovery in models module without the need to specify app_label.
Thanks mark@ and Aramgutang for work on the patch.
Diffstat (limited to 'django/db')
| -rw-r--r-- | django/db/models/base.py | 18 | ||||
| -rw-r--r-- | django/db/models/loading.py | 6 |
2 files changed, 19 insertions, 5 deletions
diff --git a/django/db/models/base.py b/django/db/models/base.py index fbfda616a1..01ba559e08 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -19,7 +19,7 @@ from django.db.models.query_utils import DeferredAttribute, deferred_class_facto from django.db.models.deletion import Collector from django.db.models.options import Options from django.db.models import signals -from django.db.models.loading import register_models, get_model +from django.db.models.loading import register_models, get_model, MODELS_MODULE_NAME from django.utils.translation import ugettext_lazy as _ from django.utils.functional import curry from django.utils.encoding import force_str, force_text @@ -86,10 +86,22 @@ class ModelBase(type): base_meta = getattr(new_class, '_meta', None) if getattr(meta, 'app_label', None) is None: - # Figure out the app_label by looking one level up. + # Figure out the app_label by looking one level up from the package + # or module named 'models'. If no such package or module exists, + # fall back to looking one level up from the module this model is + # defined in. + # For 'django.contrib.sites.models', this would be 'sites'. + # For 'geo.models.places' this would be 'geo'. + model_module = sys.modules[new_class.__module__] - kwargs = {"app_label": model_module.__name__.split('.')[-2]} + package_components = model_module.__name__.split('.') + package_components.reverse() # find the last occurrence of 'models' + try: + app_label_index = package_components.index(MODELS_MODULE_NAME) + 1 + except ValueError: + app_label_index = 1 + kwargs = {"app_label": package_components[app_label_index]} else: kwargs = {} diff --git a/django/db/models/loading.py b/django/db/models/loading.py index fb61098bfa..7280051bd8 100644 --- a/django/db/models/loading.py +++ b/django/db/models/loading.py @@ -15,6 +15,8 @@ import os __all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models', 'load_app', 'app_cache_ready') +MODELS_MODULE_NAME = 'models' + class UnavailableApp(Exception): pass @@ -98,12 +100,12 @@ class AppCache(object): self.nesting_level += 1 app_module = import_module(app_name) try: - models = import_module('.models', app_name) + models = import_module('.' + MODELS_MODULE_NAME, app_name) except ImportError: self.nesting_level -= 1 # If the app doesn't have a models module, we can just ignore the # ImportError and return no models for it. - if not module_has_submodule(app_module, 'models'): + if not module_has_submodule(app_module, MODELS_MODULE_NAME): return None # But if the app does have a models module, we need to figure out # whether to suppress or propagate the error. If can_postpone is |
