From 070d21062832f8c0a7632c5d613c805ce9898e2b Mon Sep 17 00:00:00 2001
From: Henrique Bastos <henrique@bastos.net>
Date: Sun, 5 Dec 2010 22:50:05 -0200
Subject: [PATCH] Fixes 13903
---
django/db/models/base.py | 14 ++++++++-
tests/modeltests/auto_app_label/app/models.py | 6 ++++
.../auto_app_label/app_package/models/__init__.py | 6 ++++
.../modeltests/auto_app_label/app_parent/models.py | 2 +
tests/modeltests/auto_app_label/parentmodel.py | 5 +++
tests/modeltests/auto_app_label/tests.py | 31 ++++++++++++++++++++
6 files changed, 63 insertions(+), 1 deletions(-)
create mode 100644 tests/modeltests/auto_app_label/__init__.py
create mode 100644 tests/modeltests/auto_app_label/app/__init__.py
create mode 100644 tests/modeltests/auto_app_label/app/models.py
create mode 100644 tests/modeltests/auto_app_label/app_package/__init__.py
create mode 100644 tests/modeltests/auto_app_label/app_package/models/__init__.py
create mode 100644 tests/modeltests/auto_app_label/app_parent/__init__.py
create mode 100644 tests/modeltests/auto_app_label/app_parent/models.py
create mode 100644 tests/modeltests/auto_app_label/models.py
create mode 100644 tests/modeltests/auto_app_label/parentmodel.py
create mode 100644 tests/modeltests/auto_app_label/tests.py
diff --git a/django/db/models/base.py b/django/db/models/base.py
index 18a684d..f349c71 100644
a
|
b
|
class ModelBase(type):
|
48 | 48 | # Figure out the app_label by looking one level up. |
49 | 49 | # For 'django.contrib.sites.models', this would be 'sites'. |
50 | 50 | model_module = sys.modules[new_class.__module__] |
51 | | kwargs = {"app_label": model_module.__name__.split('.')[-2]} |
| 51 | kwargs = {"app_label": get_appname_for_module(model_module)} |
52 | 52 | else: |
53 | 53 | kwargs = {} |
54 | 54 | |
… |
… |
if sys.version_info < (2, 5):
|
914 | 914 | else: |
915 | 915 | def subclass_exception(name, parents, module): |
916 | 916 | return type(name, parents, {'__module__': module}) |
| 917 | |
| 918 | def get_appname_for_module(module): |
| 919 | """ |
| 920 | Used to calculate appname for a given module |
| 921 | """ |
| 922 | module_name = module.__name__ |
| 923 | namespace = module_name.split('.') |
| 924 | |
| 925 | if len(namespace) == 1: |
| 926 | return namespace[0] |
| 927 | else: |
| 928 | return namespace[-2] |
| 929 | No newline at end of file |
diff --git a/tests/modeltests/auto_app_label/__init__.py b/tests/modeltests/auto_app_label/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/modeltests/auto_app_label/app/__init__.py b/tests/modeltests/auto_app_label/app/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/modeltests/auto_app_label/app/models.py b/tests/modeltests/auto_app_label/app/models.py
new file mode 100644
index 0000000..1141acc
-
|
+
|
|
| 1 | # coding: utf-8 |
| 2 | from django.db import models |
| 3 | |
| 4 | |
| 5 | class ModelAtNormalLevel(models.Model): |
| 6 | pass |
diff --git a/tests/modeltests/auto_app_label/app_package/__init__.py b/tests/modeltests/auto_app_label/app_package/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/modeltests/auto_app_label/app_package/models/__init__.py b/tests/modeltests/auto_app_label/app_package/models/__init__.py
new file mode 100644
index 0000000..da59b01
-
|
+
|
|
| 1 | # coding: utf-8 |
| 2 | from django.db import models |
| 3 | |
| 4 | |
| 5 | class ModelAtPackageLevel(models.Model): |
| 6 | pass |
diff --git a/tests/modeltests/auto_app_label/app_parent/__init__.py b/tests/modeltests/auto_app_label/app_parent/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/modeltests/auto_app_label/app_parent/models.py b/tests/modeltests/auto_app_label/app_parent/models.py
new file mode 100644
index 0000000..bf6816c
-
|
+
|
|
| 1 | from parentmodel import ModelAtParentLevel |
| 2 | |
diff --git a/tests/modeltests/auto_app_label/models.py b/tests/modeltests/auto_app_label/models.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/modeltests/auto_app_label/parentmodel.py b/tests/modeltests/auto_app_label/parentmodel.py
new file mode 100644
index 0000000..de15ff2
-
|
+
|
|
| 1 | from django.db import models |
| 2 | |
| 3 | |
| 4 | class ModelAtParentLevel(models.Model): |
| 5 | pass |
diff --git a/tests/modeltests/auto_app_label/tests.py b/tests/modeltests/auto_app_label/tests.py
new file mode 100644
index 0000000..f86c22c
-
|
+
|
|
| 1 | # coding: utf-8 |
| 2 | import sys, os |
| 3 | from django.conf import settings |
| 4 | from django.core.management import call_command |
| 5 | from django.db.models.loading import load_app, get_model, cache |
| 6 | from django.test import TransactionTestCase |
| 7 | |
| 8 | |
| 9 | class AutoAppLabelTest(TransactionTestCase): |
| 10 | |
| 11 | def setUp(self): |
| 12 | self.old_sys_path = sys.path[:] |
| 13 | self.old_installed_apps = settings.INSTALLED_APPS |
| 14 | |
| 15 | sys.path.append(os.path.dirname(os.path.abspath(__file__))) |
| 16 | settings.INSTALLED_APPS = ('app', 'app_package', 'app_parent') |
| 17 | map(load_app, settings.INSTALLED_APPS) |
| 18 | call_command('syncdb', verbosity=0) |
| 19 | |
| 20 | def tearDown(self): |
| 21 | settings.INSTALLED_APPS = self.old_installed_apps |
| 22 | sys.path = self.old_sys_path |
| 23 | |
| 24 | def test_model_at_normal_level(self): |
| 25 | self.assertTrue(get_model('app', 'modelatnormallevel')) |
| 26 | |
| 27 | def test_model_at_package_level(self): |
| 28 | self.assertTrue(get_model('app_package', 'modelatpackagelevel')) |
| 29 | |
| 30 | def test_model_at_parent_module(self): |
| 31 | self.assertTrue(get_model('parentmodel', 'modelatparentlevel')) |