Ticket #3420: db.models.loading.diff

File db.models.loading.diff, 1.2 KB (added by egmanoj@…, 8 years ago)

Patch for displaying details such as file name, line number etc. for import errors that occur during 'validate'.

  • models/loading.py

    44from django.core.exceptions import ImproperlyConfigured
    55import sys
    66import os
     7import traceback
    89__all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models')
    2930                load_app(app_name)
    3031            except Exception, e:
    3132                # Problem importing the app
    32                 _app_errors[app_name] = e
     33                _app_errors[app_name] = format_exception(e)
    3334    return _app_list
     36def format_exception(exception):
     37    "Adds traceback information to the exception to assist debugging."
     38    filename, line_number, function_name, text = traceback.extract_tb(sys.exc_info()[-1])[-1]
     39    return "%s\n\tFile %s, line %s, in %s\n\t\t%s" % (exception, filename, line_number, function_name, text)
    3541def get_app(app_label, emptyOK=False):
    3642    "Returns the module containing the models for the given app_label. If the app has no models in it and 'emptyOK' is True, returns None."
    3743    get_apps() # Run get_apps() to populate the _app_list cache. Slightly hackish.
Back to Top