Allow importing of nested model modules.

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@1663 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Robert Wittams 2005-12-15 00:47:30 +00:00
parent a57b462757
commit 9701daeb72
3 changed files with 22 additions and 20 deletions

View File

@ -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:

View File

@ -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):

View File

@ -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 '<Options for %s>' % 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."