RuntimeError:Model do do do do do app_label和installed_apps中的应用
我正在Django编写一个应用程序,我正在尝试进行一些单元测试 但是我似乎找不到为什么测试失败 那是测试页面:
import re
from django.test import TestCase
from django.urls import reverse
from . import models
class BasicTests(TestCase):
def test_firstname(self):
print('test11')
acc = models.Accounts()
acc.first_name = 'Moran'
self.assertTrue(len(acc.id) <= 9, 'Check name is less than 50 digits long')
self.assertFalse(len(acc.id) > 50, 'Check name is less than 50 digits long')
我遇到的错误是:
RuntimeError:模型类doggiesitter.accounts.models.accounts 不会声明明确的app_label,也不在 installed_apps
就是我安装的应用程序:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts'
]
I am writing an app in Django and I'm trying to do some unit testing
but I can't seem to find why the test is failing
that is the test page:
import re
from django.test import TestCase
from django.urls import reverse
from . import models
class BasicTests(TestCase):
def test_firstname(self):
print('test11')
acc = models.Accounts()
acc.first_name = 'Moran'
self.assertTrue(len(acc.id) <= 9, 'Check name is less than 50 digits long')
self.assertFalse(len(acc.id) > 50, 'Check name is less than 50 digits long')
the error i get is :
RuntimeError: Model class DoggieSitter.accounts.models.Accounts
doesn't declare an explicit app_label and isn't in an application in
INSTALLED_APPS
thats my installed app:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts'
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将上面的第4行更改为明确导入,例如
从doggiesitter.accounts导入模型
我都会在运行
tests.py
的测试时都有此问题。 >来自.models导入modelName 。搜索了一个小时左右后,我偶然发现了这个答案是对我的教程的确切答案。 >。就我而言,我正在尝试
。我的项目结构如下,因此我从apps.recipes.models导入食谱
更改为,并且测试现在运行良好。这很可惜,因为我宁愿继续使用相对进口。
PS-似乎也起作用的另一种更明确的方法是:
...但是我认为我更喜欢简单的显式导入语句。
Try changing line 4 above to an explicit import such as
from DoggieSitter.accounts import models
I had this problem whenever running tests where the
tests.py
had a relative import suchfrom .models import ModelName
. After searching for an hour or so, I stumbled on this answer to exactly the tutorial I was following.In my case, I was trying
from .models import Recipe
. My project structure is as below, so I changed tofrom apps.recipes.models import Recipe
and the test now runs fine. It's a shame because I'd prefer to keep using relative imports.PS - another even more explicit way that also seems to work is:
... but I think I prefer the simpler explicit import statement.