summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2006-06-25 14:49:34 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2006-06-25 14:49:34 +0000
commitfd702fe03422dd7a0d84eeac5c990c5cc46470bd (patch)
treeb553221651e773f04622efb9acaebec93e6de993
parent23c24fc08bd05a4b9371df7837b8819f29142ac5 (diff)
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). git-svn-id: http://code.djangoproject.com/svn/django/trunk@3202 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/db/models/loading.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/django/db/models/loading.py b/django/db/models/loading.py
index 96e864cffc..3602090650 100644
--- a/django/db/models/loading.py
+++ b/django/db/models/loading.py
@@ -2,6 +2,8 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
+import sys
+import os
__all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models')
@@ -91,4 +93,14 @@ def register_models(app_label, *models):
# in the _app_models dictionary
model_name = model._meta.object_name.lower()
model_dict = _app_models.setdefault(app_label, {})
+ if model_dict.has_key(model_name):
+ # The same model may be imported via different paths (e.g.
+ # appname.models and project.appname.models). We use the source
+ # filename as a means to detect identity.
+ fname1 = os.path.normpath(sys.modules[model.__module__].__file__)
+ fname2 = os.path.normpath(sys.modules[model_dict[model_name].__module__].__file__)
+ # Since the filename extension could be .py the first time and .pyc
+ # or .pyo the second time, ignore the extension when comparing.
+ if os.path.splitext(fname1)[0] == os.path.splitext(fname2)[0]:
+ continue
model_dict[model_name] = model