From 9701daeb7257a576a380ba3579e41b7c5c78b9d9 Mon Sep 17 00:00:00 2001 From: Robert Wittams Date: Thu, 15 Dec 2005 00:47:30 +0000 Subject: [PATCH] Allow importing of nested model modules. git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@1663 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- .../admin/templatetags/adminapplist.py | 1 - django/core/management.py | 11 ++++--- django/db/models/__init__.py | 30 +++++++++++-------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/django/contrib/admin/templatetags/adminapplist.py b/django/contrib/admin/templatetags/adminapplist.py index 21c343ed19..90396891cb 100644 --- a/django/contrib/admin/templatetags/adminapplist.py +++ b/django/contrib/admin/templatetags/adminapplist.py @@ -15,7 +15,6 @@ class AdminApplistNode(template.Node): for app in models.get_installed_model_modules(): app_label = app.__name__[app.__name__.rindex('.')+1:] has_module_perms = user.has_module_perms(app_label) - if has_module_perms: model_list = [] for m in app._MODELS: diff --git a/django/core/management.py b/django/core/management.py index c4c4e34676..85d4071337 100644 --- a/django/core/management.py +++ b/django/core/management.py @@ -371,8 +371,7 @@ def init(): "Initializes the database with auth and core." try: from django.db import backend, connection, models - auth = models.get_app('auth') - core = models.get_app('core') + from django.models import auth, core cursor = connection.cursor() for sql in get_sql_create(core) + get_sql_create(auth) + get_sql_initial_data(core) + get_sql_initial_data(auth): cursor.execute(sql) @@ -642,8 +641,8 @@ def get_validation_errors(outfile): e = ModelErrorCollection(outfile) module_list = models.get_installed_model_modules() for module in module_list: - for mod in module._MODELS: - opts = mod._meta + for cls in module._MODELS: + opts = cls._meta # Do field-specific validation. for f in opts.fields: @@ -690,8 +689,8 @@ def get_validation_errors(outfile): for fn in opts.admin.list_display: try: f = opts.get_field(fn) - except models.FieldDoesNotExist: - if not hasattr(mod, fn) or not callable(getattr(mod, fn)): + except models.FieldDoesNotExist: + if not hasattr(cls, fn) or not callable(getattr(cls, fn)): e.add(opts, '"admin.list_display" refers to %r, which isn\'t a field or method.' % fn) else: if isinstance(f, models.ManyToManyField): diff --git a/django/db/models/__init__.py b/django/db/models/__init__.py index 590c866def..e1c3367e8b 100644 --- a/django/db/models/__init__.py +++ b/django/db/models/__init__.py @@ -69,13 +69,14 @@ def orderlist2sql(order_list, opts, prefix=''): output.append('%s%s ASC' % (prefix, backend.quote_name(orderfield2column(f, opts)))) return ', '.join(output) -def get_module(app_label, module_name): - return __import__('%s.%s.%s' % (MODEL_PREFIX, app_label, module_name), '', '', ['']) +#def get_module(app_label, module_name): +# return __import__('%s.%s.%s' % (MODEL_PREFIX, app_label, module_name), '', '', ['']) -def get_app(app_label): - return __import__('%s.%s' % (MODEL_PREFIX, app_label), '', '', ['']) +#def get_app(app_label): +# return __import__('%s.%s' % (MODEL_PREFIX, app_label), '', '', ['']) _installed_models_cache = None + def get_installed_models(): """ Returns a list of installed "models" packages, such as foo.models, @@ -88,11 +89,19 @@ def get_installed_models(): for a in settings.INSTALLED_APPS: try: _installed_models_cache.append(__import__(a + '.models', '', '', [''])) - except ImportError: + except ImportError, e: pass return _installed_models_cache _installed_modules_cache = None + +def add_model_module(mod, modules): + if hasattr(mod, '_MODELS'): + modules.append(mod) + for name in getattr(mod, '__all__', []): + submod = __import__("%s.%s" % ( mod.__name__, name),'','',['']) + add_model_module(submod, modules) + def get_installed_model_modules(core_models=None): """ Returns a list of installed models, such as django.models.core, @@ -107,12 +116,7 @@ def get_installed_model_modules(core_models=None): for submodule in (core_models or []): _installed_modules_cache.append(__import__('django.models.%s' % submodule, '', '', [''])) for mod in get_installed_models(): - try: - mod._MODELS - except AttributeError: - pass # Skip model modules that don't actually have models in them. - else: - _installed_modules_cache.append(mod) + add_model_module(mod, _installed_modules_cache) return _installed_modules_cache class LazyDate: @@ -416,8 +420,8 @@ class Options: def __repr__(self): return '' % self.module_name - def get_model_module(self): - return get_module(self.app_label, self.module_name) + # def get_model_module(self): + # return get_module(self.app_label, self.module_name) def get_content_type_id(self): "Returns the content-type ID for this object type."