8000 [soc2010/app-loading] load custom models if specified with models_pat… · alex-python/django@2b7bfac · GitHub
[go: up one dir, main page]

Skip to content

Commit 2b7bfac

Browse files
committed
[soc2010/app-loading] load custom models if specified with models_path attribute
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/app-loading@13570 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 9cf522e commit 2b7bfac

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

django/core/apps.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def __init__(self, name):
2525
# errors raised when trying to import the app
2626
self.errors = []
2727
self.models = []
28-
self.models_module = None
2928

3029
def __repr__(self):
3130
return '<App: %s>' % self.name
@@ -112,8 +111,15 @@ def load_app(self, app_name, can_postpone=False):
112111
app_instance = app_class(app_instance_name)
113112
self.app_instances.append(app_instance)
114113

114+
# check if the app instance specifies a path to models
115+
# if not, we use the models.py file from the package dir
115116
try:
116-
models = import_module('.models', app_name)
117+
models_path = app_instance.models_path
118+
except AttributeError:
119+
models_path = '%s.models' % app_name
120+
121+
try:
122+
models = import_module(models_path)
117123
except ImportError:
118124
self.nesting_level -= 1
119125
# If the app doesn't have a models module, we can just ignore the
@@ -135,9 +141,7 @@ def load_app(self, app_name, can_postpone=False):
135141
raise
136142

137143
self.nesting_level -= 1
138-
app = self.find_app(app_name.split('.')[-1])
139-
if app and models is not app.models_module:
140-
app.models_module = models
144+
app_instance.models_module = models
141145
return models
142146

143147
def find_app(self, name):
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.core.apps import App
2+
3+
class MyApp(App):
4+
models_path = 'model_app.othermodels'
5+
6+
def __repr__(self):
7+
return '<MyApp: %s>' % self.name
8+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.db import models
2+
3+
class Person(models.Model):
4+
first_name = models.CharField(max_length=30)
5+
last_name = models.CharField(max_length=30)

tests/appcachetests/runtests.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,9 @@ def test_without_models(self):
221221
app = cache.app_instances[0]
222222
self.assertEqual(len(cache.app_instances), 1)
223223
self.assertEqual(app.name, 'nomodel_app')
224-
self.assertEqual(app.models_module, None)
225224
self.assertEqual(rv, None)
226225

227-
def test_load_app_custom(self):
226+
def test_custom_app(self):
228227
"""
229228
Test that a custom app instance is created if the function
230229
gets passed a classname
@@ -235,10 +234,18 @@ def test_load_app_custom(self):
235234
self.assertEqual(len(cache.app_instances), 1)
236235
self.assertEqual(app.name, 'nomodel_app')
237236
self.assertTrue(isinstance(app, MyApp))
238-
self.assertEqual(app.models_module, None)
239237
self.assertEqual(rv, None)
240238

241-
def test_load_app_twice(self):
239+
def test_custom_models_path(self):
240+
"""
241+
Test that custom models are imported correctly
242+
"""
243+
rv = cache.load_app('model_app.MyApp')
244+
app = cache.app_instances[0]
245+
self.assertEqual(app.models_module.__name__, 'model_app.othermodels')
246+
self.assertEqual(rv.__name__, 'model_app.othermodels')
247+
248+
def test_twice(self):
242249
"""
243250
Test that loading an app twice results in only one app instance
244251
"""
@@ -248,7 +255,7 @@ def test_load_app_twice(self):
248255
self.assertEqual(rv.__name__, 'model_app.models')
249256
self.assertEqual(rv2.__name__, 'model_app.models')
250257

251-
def test_load_app_importerror(self):
258+
def test_importerror(self):
252259
"""
253260
Test that an ImportError exception is raised if a package cannot
254261
be imported

0 commit comments

Comments
 (0)
0