mirror of
https://github.com/django/django.git
synced 2025-10-25 22:56:12 +00:00
[1.8.x] Fixed #24287 -- Added friendly error if a model is in a models.py outside an installed app.
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
from django.apps import apps
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.db import models
|
||||
from django.test import TestCase
|
||||
from django.test.utils import extend_sys_path
|
||||
from django.utils._os import upath
|
||||
@@ -75,3 +78,26 @@ class GetModelsTest(TestCase):
|
||||
self.assertNotIn(
|
||||
"NotInstalledModel",
|
||||
[m.__name__ for m in apps.get_models()])
|
||||
|
||||
def test_exception_raised_if_model_declared_outside_app(self):
|
||||
|
||||
class FakeModule(models.Model):
|
||||
__name__ = str("models_that_do_not_live_in_an_app")
|
||||
|
||||
sys.modules['models_not_in_app'] = FakeModule
|
||||
|
||||
def declare_model_outside_app():
|
||||
models.base.ModelBase.__new__(
|
||||
models.base.ModelBase,
|
||||
str('Outsider'),
|
||||
(models.Model,),
|
||||
{'__module__': 'models_not_in_app'})
|
||||
|
||||
msg = (
|
||||
'Unable to detect the app label for model "Outsider." '
|
||||
'Ensure that its module, "models_that_do_not_live_in_an_app", '
|
||||
'is located inside an installed app.'
|
||||
)
|
||||
|
||||
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
||||
declare_model_outside_app()
|
||||
|
||||
Reference in New Issue
Block a user