使用自定义用户模型进行单元测试

时间:2014-04-09 05:28:19

标签: python django

尝试运行unittes时出现以下错误:

命令:

python manage.py test wall

输出:

wall.wall: 'author' has a relation with model accounts.Account, which has either not been installed or is abstract.

(list of other models, which have their links to accounts.Account)

代码段:

因此,我们以下列方式从AbstractBaseUser继承:

帐户/ models.py:

from wall.models import Wall
...

class Account(AbstractBaseUser, PermissionsMixin):
    username = models.CharField(...)
    name = models.CharField(...)
    wall = GenericRelation(Wall, content_type_field='object_type', object_id_field='object_id')
    ...

class UserAccount(Account):
    class Meta:
        proxy = True
        verbose_name = 'User'
        verbose_name_plural = 'Users'

    def save(self, *args, **kwargs):
        self.is_organization = False
        self.is_charity = False

        super(UserAccount, self).save(*args, **kwargs)

壁/ models.py

class Wall(TimeStampedModel):
    objects = WallManager()

    author = models.ForeignKey(settings.AUTH_USER_MODEL)

    title = models.CharField(max_length=140)
    content = models.TextField()

settings.AUTH_USER_MODEL ='accounts.Account' - 在设置文件中。

壁/ tests.py

from django.test import TestCase
from accounts.models import UserAccount
from wall.models import Wall

class WallTests(TestCase):

    def test_simple_wall_post(self):    
        jacob = UserAccount.objects.create_user(
            username='jacob', email='jacob@...', password='top_secret')
        kyle = self.user = UserAccount.objects.create_user(
            username='kyle', email='kyle@...', password='top_secret_2')

        Wall.objects.create_post(author=jacob, title="I'm posting on your wall",
                                 content="this is a random message",
                                 object=kyle)
        wall_item = Wall.objects.get(author=jacob, object=kyle)
        self.assertEqual(wall_item.author, jacob)

移动时

from accounts.models import UserAccount

test_simple_wall_post()功能中,问题就消失了。这显然不方便。那导致抽象bug的原因是什么?我导入了非抽象模型,所以对我来说很奇怪。

0 个答案:

没有答案