Skip to content

Commit

Permalink
Fixes #1812 -- Added model validity checks to ensure that models.py e…
Browse files Browse the repository at this point in the history
…xists, and has been successfully imported for all INSTALLED_APPS. Previous behaviour was to silently ignore empty/problem models, which resulted in the display of an admin page that doesn't display a supposedly installed model. Thanks to Ian Holsman for the original report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3201 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed Jun 25, 2006
1 parent dc47330 commit 23c24fc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
11 changes: 8 additions & 3 deletions django/core/management.py
Expand Up @@ -803,9 +803,9 @@ def __init__(self, outfile=sys.stdout):
self.errors = []
self.outfile = outfile

def add(self, opts, error):
self.errors.append((opts, error))
self.outfile.write(style.ERROR("%s.%s: %s\n" % (opts.app_label, opts.module_name, error)))
def add(self, context, error):
self.errors.append((context, error))
self.outfile.write(style.ERROR("%s: %s\n" % (context, error)))

def get_validation_errors(outfile, app=None):
"""
Expand All @@ -814,9 +814,14 @@ def get_validation_errors(outfile, app=None):
Returns number of errors.
"""
from django.db import models
from django.db.models.loading import get_app_errors
from django.db.models.fields.related import RelatedObject

e = ModelErrorCollection(outfile)

for (app_name, error) in get_app_errors().items():
e.add(app_name, error)

for cls in models.get_models(app):
opts = cls._meta

Expand Down
18 changes: 13 additions & 5 deletions django/db/models/loading.py
Expand Up @@ -10,6 +10,9 @@
_app_models = {} # Dictionary of models against app label
# Each value is a dictionary of model name: model class
# Applabel and Model entry exists in cache when individual model is loaded.
_app_errors = {} # Dictionary of errors that were experienced when loading the INSTALLED_APPS
# Key is the app_name of the model, value is the exception that was raised
# during model loading.
_loaded = False # Has the contents of settings.INSTALLED_APPS been loaded?
# i.e., has get_apps() been called?

Expand All @@ -22,11 +25,9 @@ def get_apps():
for app_name in settings.INSTALLED_APPS:
try:
load_app(app_name)
except ImportError:
pass # Assume this app doesn't have a models.py in it.
# GOTCHA: It may have a models.py that raises ImportError.
except AttributeError:
pass # This app doesn't have a models.py in it.
except Exception, e:
# Problem importing the app
_app_errors[app_name] = e
return _app_list

def get_app(app_label):
Expand All @@ -39,10 +40,17 @@ def get_app(app_label):

def load_app(app_name):
"Loads the app with the provided fully qualified name, and returns the model module."
global _app_list
mod = __import__(app_name, '', '', ['models'])
if mod.models not in _app_list:
_app_list.append(mod.models)
return mod.models

def get_app_errors():
"Returns the map of known problems with the INSTALLED_APPS"
global _app_errors
get_apps() # Run get_apps() to populate the _app_list cache. Slightly hackish.
return _app_errors

def get_models(app_mod=None):
"""
Expand Down
5 changes: 4 additions & 1 deletion django/db/models/options.py
Expand Up @@ -87,7 +87,10 @@ def add_field(self, field):

def __repr__(self):
return '<Options for %s>' % self.object_name


def __str__(self):
return "%s.%s" % (self.app_label, self.module_name)

def get_field(self, name, many_to_many=True):
"Returns the requested field by name. Raises FieldDoesNotExist on error."
to_search = many_to_many and (self.fields + self.many_to_many) or self.fields
Expand Down

0 comments on commit 23c24fc

Please sign in to comment.