Django

Code

Changeset 3201

Show
Ignore:
Timestamp:
06/24/06 23:24:15 (2 years ago)
Author:
russellm
Message:

Fixes #1812 -- Added model validity checks to ensure that models.py exists, 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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/management.py

    r3195 r3201  
    804804        self.outfile = outfile 
    805805 
    806     def add(self, opts, error): 
    807         self.errors.append((opts, error)) 
    808         self.outfile.write(style.ERROR("%s.%s: %s\n" % (opts.app_label, opts.module_name, error))) 
     806    def add(self, context, error): 
     807        self.errors.append((context, error)) 
     808        self.outfile.write(style.ERROR("%s: %s\n" % (context, error))) 
    809809 
    810810def get_validation_errors(outfile, app=None): 
     
    815815    """ 
    816816    from django.db import models 
     817    from django.db.models.loading import get_app_errors 
    817818    from django.db.models.fields.related import RelatedObject 
    818819 
    819820    e = ModelErrorCollection(outfile) 
     821     
     822    for (app_name, error) in get_app_errors().items(): 
     823        e.add(app_name, error) 
     824         
    820825    for cls in models.get_models(app): 
    821826        opts = cls._meta 
  • django/trunk/django/db/models/loading.py

    r3195 r3201  
    1111                 # Each value is a dictionary of model name: model class 
    1212                 # Applabel and Model entry exists in cache when individual model is loaded. 
     13_app_errors = {} # Dictionary of errors that were experienced when loading the INSTALLED_APPS 
     14                 # Key is the app_name of the model, value is the exception that was raised 
     15                 # during model loading. 
    1316_loaded = False  # Has the contents of settings.INSTALLED_APPS been loaded?  
    1417                 # i.e., has get_apps() been called? 
     
    2326            try: 
    2427                load_app(app_name) 
    25             except ImportError: 
    26                 pass # Assume this app doesn't have a models.py in it. 
    27                      # GOTCHA: It may have a models.py that raises ImportError. 
    28             except AttributeError: 
    29                 pass # This app doesn't have a models.py in it. 
     28            except Exception, e: 
     29                # Problem importing the app 
     30                _app_errors[app_name] = e 
    3031    return _app_list 
    3132 
     
    4041def load_app(app_name): 
    4142    "Loads the app with the provided fully qualified name, and returns the model module." 
     43    global _app_list 
    4244    mod = __import__(app_name, '', '', ['models']) 
    4345    if mod.models not in _app_list: 
    4446        _app_list.append(mod.models) 
    4547    return mod.models 
     48 
     49def get_app_errors(): 
     50    "Returns the map of known problems with the INSTALLED_APPS" 
     51    global _app_errors 
     52    get_apps() # Run get_apps() to populate the _app_list cache. Slightly hackish. 
     53    return _app_errors 
    4654     
    4755def get_models(app_mod=None): 
  • django/trunk/django/db/models/options.py

    r3113 r3201  
    8888    def __repr__(self): 
    8989        return '<Options for %s>' % self.object_name 
    90  
     90         
     91    def __str__(self): 
     92        return "%s.%s" % (self.app_label, self.module_name) 
     93         
    9194    def get_field(self, name, many_to_many=True): 
    9295        "Returns the requested field by name. Raises FieldDoesNotExist on error."