我需要创建 10 个示例用户 ( User
),每个用户必须有 50 个文档 ( Doc
)。如何使用 factoryboy 在 tests.py 中执行此操作?
#factories.py
from app_name.models import *
import factory
from datetime import datetime, timedelta, time
from django.contrib.auth.models import User
class UserFactory(factory.Factory):
FACTORY_FOR = User
username = factory.Sequence(lambda n: 'User ' + n)
email = 'demo@mail.com'
password = '1234567'
class DocFactory(factory.Factory):
FACTORY_FOR = Doc
user = factory.SubFactory(UserFactory)
kategories = '1'
doc_number = '12345678'
date_join = factory.Sequence(lambda n:(datetime.now() + timedelta(days=n)).date(), int)
在我的tests.py中:
from django.test import TestCase
from django_dynamic_fixture import G
from factories import *