Django

Code

Changeset 3202

Show
Ignore:
Timestamp:
06/25/06 09:49:34 (2 years ago)
Author:
mtredinnick
Message:

Fixed #1796 -- only load a single copy of each model, even when it is
referenced via different import paths. Solves a problem with many-to-many
relations being set up incorrectly. Thanks to Curtis Thompson, Luke Plant and
Simon Willison for some excellent debugging of the problem. Refs #2232 (may
also have fixed that).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/loading.py

    r3201 r3202  
    33from django.conf import settings 
    44from django.core.exceptions import ImproperlyConfigured 
     5import sys 
     6import os 
    57 
    68__all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models') 
     
    9294        model_name = model._meta.object_name.lower() 
    9395        model_dict = _app_models.setdefault(app_label, {}) 
     96        if model_dict.has_key(model_name): 
     97            # The same model may be imported via different paths (e.g. 
     98            # appname.models and project.appname.models). We use the source 
     99            # filename as a means to detect identity. 
     100            fname1 = os.path.normpath(sys.modules[model.__module__].__file__) 
     101            fname2 = os.path.normpath(sys.modules[model_dict[model_name].__module__].__file__) 
     102            # Since the filename extension could be .py the first time and .pyc 
     103            # or .pyo the second time, ignore the extension when comparing. 
     104            if os.path.splitext(fname1)[0] == os.path.splitext(fname2)[0]: 
     105                continue 
    94106        model_dict[model_name] = model