模型类myproject.apps.accounts.models.User未声明显式的app_label,并且不在INSTALLED_APPS的应用程序中

时间:2016-08-11 12:52:32

标签: django unit-testing

我正在使用django的AbstractUser创建我的自定义用户以及扩展BaseUserManager的自定义管理器。但我还没有添加任何额外的字段。所以它看起来像这样:

# mypoject/apps/accounts/models.py

from django.contrib.auth.models import AbstractUser
from myproject.apps.accounts.managers import UserManager

class User(AbstractUser):
    objects = UserManager

我为它写了一个单元测试,它看起来像这样:

# myproject/apps/accounts/tests/test_models.py

from unittest import TestCase

class UserTests(TestCase):
    def test_create_user(self):
        from myproject.apps.accounts.models import User
        user = User.objects.create_user(username='mock_user')
        self.assertIsInstance(user)

但是当我运行测试时,我收到此错误消息:

RuntimeError: Model class myproject.apps.accounts.models.User doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

PS:我的帐户应用程序位于INSTALLED_APPS数组中且工作正常。

1 个答案:

答案 0 :(得分:1)

这可能是因为here描述的问题以及您需要分配管理员实例的objects

class User(AbstractUser):
    objects = UserManager()