@Andrew-Magee 解决方案有效,但这里的解决方案在文档中描述:
import factory
from django.contrib.auth.models import User
#or
#from somewhere import CustomUser as User
class UserFactory(factory.DjangoModelFactory):
FACTORY_FOR = User
username = 'UserFactory'
email = 'test@example.com'
password = factory.PostGenerationMethodCall('set_password', 'password')
Django控制台:
>>> from tests.factories import UserFactory
>>> from django.contrib.auth.models import check_password
>>> user = UserFactory()
>>> user.email
'test@example.com'
>>> check_password('password', user.password)
True
>>> user2 = UserFactory(username="SecondUserFactory", email='otheremail@example.com', password="ComplexPasswordMuchLonger!")
>>> user2.email
'otheremail@example.com'
>>> check_password('ComplexPasswordMuchLonger!', user2.password)
True