Skip to content

Commit 48dd5f1

Browse files
committed
Fixed #13335: Adjusted the r12950 fix to properly handle import errors resulting from nested calls to load_app.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12972 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent d8910e9 commit 48dd5f1

File tree

10 files changed

+43
-5
lines changed

10 files changed

+43
-5
lines changed

django/db/models/loading.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,23 @@ def load_app(self, app_name, can_postpone=False):
7575
app_module = import_module(app_name)
7676
try:
7777
imp.find_module('models', app_module.__path__)
78+
except ImportError:
79+
self.nesting_level -= 1
80+
# App has no models module, that's not a problem.
81+
return None
82+
try:
83+
models = import_module('.models', app_name)
7884
except ImportError:
7985
self.nesting_level -= 1
8086
if can_postpone:
81-
# Either the app has no models, or the package is still being
87+
# Either the app has an error, or the package is still being
8288
# imported by Python and the model module isn't available yet.
8389
# We will check again once all the recursion has finished (in
8490
# populate).
8591
self.postponed.append(app_name)
86-
return None
87-
models = import_module('.models', app_name)
92+
return None
93+
else:
94+
raise
8895
self.nesting_level -= 1
8996
if models not in self.app_store:
9097
self.app_store[models] = len(self.app_store)

tests/regressiontests/admin_scripts/complex_app/__init__.py

Whitespace-only changes.

tests/regressiontests/admin_scripts/complex_app/admin/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
from admin_scripts.complex_app.models.foo import Foo
3+
admin.site.register(Foo)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from admin_scripts.complex_app.models.bar import Bar
2+
from admin_scripts.complex_app.models.foo import Foo
3+
4+
__all__ = ['Foo', 'Bar']
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.db import models
2+
3+
from ..admin import foo
4+
class Bar(models.Model):
5+
name = models.CharField(max_length=5)
6+
class Meta:
7+
app_label = 'complex_app'
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.db import models
2+
3+
class Foo(models.Model):
4+
name = models.CharField(max_length=5)
5+
class Meta:
6+
app_label = 'complex_app'

tests/regressiontests/admin_scripts/simple_app/__init__.py

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from admin_scripts.complex_app.models.bar import Bar

tests/regressiontests/admin_scripts/tests.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ def test_custom_command_with_environment(self):
957957
self.assertOutput(err, "Unknown command: 'noargs_command'")
958958

959959

960-
class ManageValidateImportErrorsReported(AdminScriptTestCase):
960+
class ManageValidate(AdminScriptTestCase):
961961
def tearDown(self):
962962
self.remove_settings('settings.py')
963963

@@ -976,7 +976,17 @@ def test_broken_app(self):
976976
out, err = self.run_manage(args)
977977
self.assertNoOutput(out)
978978
self.assertOutput(err, 'ImportError')
979-
979+
980+
def test_complex_app(self):
981+
"manage.py validate does not raise an ImportError validating a complex app with nested calls to load_app"
982+
self.write_settings('settings.py',
983+
apps=['admin_scripts.complex_app', 'admin_scripts.simple_app'],
984+
sdict={'DEBUG': True})
985+
args = ['validate']
986+
out, err = self.run_manage(args)
987+
self.assertNoOutput(err)
988+
self.assertOutput(out, '0 errors found')
989+
980990

981991
##########################################################################
982992
# COMMAND PROCESSING TESTS

0 commit comments

Comments
 (0)