我对下面的教程有疑问。我们已经到了测试的重点,运行
时我仍然出现错误python manage.py test
这是我的错误:
(restapi) chrismaltez@Chriss-MacBook-Pro:~/Desktop/pycharmprojects/UDEMY/restapi/src$ python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
..E
======================================================================
ERROR: src.status.tests (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: src.status.tests
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 428, in _find_test_path
module = self._get_module_from_name(name)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 369, in _get_module_from_name
__import__(name)
File "/Users/chrismaltez/Desktop/pycharmprojects/UDEMY/restapi/src/status/tests.py", line 6, in <module>
from .models import Status
File "/Users/chrismaltez/Desktop/pycharmprojects/UDEMY/restapi/src/status/models.py", line 20, in <module>
class Status(models.Model): #fb status, instagram post, tween, linkedin post
File "/Users/chrismaltez/Desktop/pycharmprojects/UDEMY/restapi/lib/python3.6/site-packages/django/db/models/base.py", line 118, in __new__
"INSTALLED_APPS." % (module, name)
RuntimeError: Model class src.status.models.Status doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
----------------------------------------------------------------------
Ran 3 tests in 0.096s
我不明白为什么会收到此错误,因为我在此处已安装的应用程序中具有状态:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#third party
'rest_framework',
#local
'accounts',
'status',
'updates',
]
然后这是我的模型,它说我有一个错误:
from django.conf import settings
from django.db import models
def upload_status_image(instance, filename):
return "status/{user}/{filename}".format(user=instance.user, filename=filename)
class StatusQuerySet(models.QuerySet):
pass
class StatusManager(models.Manager):
def get_queryset(self):
return StatusQuerySet(self.model, using=self._db)
# Create your models here.
class Status(models.Model): #fb status, instagram post, tween, linkedin post
user = models.ForeignKey(settings.AUTH_USER_MODEL) # user instance .save
content = models.TextField(null=True, blank=True)
image = models.ImageField(upload_to=upload_status_image, null=True, blank=True) # great 3rd part package = django storanges --> AWS#pip install pillow to handle images
updated = models.DateTimeField(auto_now=True)
timestamp = models.DateTimeField(auto_now_add=True)
objects = StatusManager
def __str__(self):
return str(self.content)[:50]
class Meta:
verbose_name = "Status post"
verbose_name_plural = "Status posts"
@property
def owner(self):
return self.user
我正在django 1.11.8的Mac上运行。
当我运行makemigrations时,没有任何错误。当我运行服务器时,我没有任何错误。我已经尝试过此solution,但没有任何效果。
任何帮助都将受到赞赏。
-更新- 文件树:
restapi
|-bin
|-include
|-lib
|-scripts
|-src
|-accounts
| |-api
| | |-user
| | | |-__init__.py
| | | |-serializers.py
| | | |-urls.py
| | | |-views.py
| | |-__init__.py
| | |-permissions.py
| | |-serializers.py
| | |-test_user_api.py
| | |-urls.py
| | |-utils.py
| | |-views.py
| |-+migrations
| |-__init__.py
| |-admin.py
| |-apps.py
| |-models.py
| |-tests.py
| |-views.py
|-cfeapi
| |-restconf
| | |-__init__.py
| | |-main.py
| | |-pagination.py
| |-__init__.py
| |-mixins.py
| |-settings.py
| |-urls.py
| |-wsgi.py
|-status
|-api
| |-__init__.py
| |-serializers.py
| |-urls.py
| |-views.py
|+migrations
|-__init__.py
|-admin.py
|-apps.py
|-forms.py
|-models.py
|-tests.py
|-views.py
答案 0 :(得分:2)
在对stackO进行更多爬网之后,我发现了这一点。这是一个简单的解决方法,我从这里得到了这个主意: website w/ answer 。事实证明答案只是将相对进口改为取消进口
在status / tests.py中,代码行必须更改为:
from .models import Status
收件人:
from status.models import Status
我仍然觉得很奇怪,它抛出了一个错误,但是accounts / tests.py很好。
答案 1 :(得分:1)
我遇到了这个问题,在我未直接指定的路径中,我得到了一个文件夹的ModuleNotFoundError。
我正在PyCharm中运行测试文件。
此修复程序将主入口点添加到我的测试文件中:
if __name__ == '__main__':
unittest.main()
答案 2 :(得分:0)
尝试诸如python manage.py test --pattern="*_models.py"
答案 3 :(得分:0)
也测试包的名称不应与源包的名称相同。例如:
- src
- utils
- utilities.py
- tests
- utils
- test_utilities.py
此设置可能导致错误,尽管它取决于您的PYTHONPATH以及运行测试的位置。请注意在utils
和src
目录中都有test
软件包。重命名其中一个软件包似乎可以解决问题,例如:utils
和test_utils
。
不是直接面对OP所面临的情况,但我认为值得一提。