我有一个问题跟随我的教程。我们已经到了测试点,我在运行时仍然遇到错误
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但没有任何效果。
任何和所有的帮助表示赞赏。
-update- filetree:
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
我在stackO上进行了更多爬行后想出来了。这是一个简单的修复,我从这里得到了这个想法:qazxsw poi。事实证明,答案只是将相对导入更改为绝对导入
在status / tests.py中,代码行必须从以下位置更改:
website w/ answer
至:
from .models import Status
我仍觉得很奇怪它抛出了一个错误但是accounts / tests.py很好。
我遇到了这个问题,我在路径中找到了一个我没有直接指定的文件夹的ModuleNotFoundError。
我在PyCharm中运行我的测试文件。
修复程序是将主入口点添加到我的测试文件中:
from status.models import Status