创建工厂对象时如何设置SubFactory的值
作者:互联网
是否可以在不创建两个Factory对象的情况下设置SubFactory的值?
例如,我有两个工厂;
class UserFactory(factory.DjangoModelFactory):
FACTORY_FOR = 'mysite.user'
name = "Mark"
class MyFactory(factory.DjangoModelFactory):
FACTORY_FOR = 'mysite.myfactory'
user = factory.SubFactory(UserFactory)
我想创建MyFactory()并同时设置user.name的值.
您必须先创建user = UserFactory.create(name =“ John”)还是从args到MyFactory()的所有代码都可以全部完成?
目前,在测试中,我得到了以下内容:
def setUp(self):
user = factories.UserFactory(name="John")
myfactory = factories.MyFactory(user=user)
解决方法:
根据documentation,您可以直接在外部工厂定义中定义SubFactory字段:
factories.MyFactory(user__name="John")
希望能有所帮助.
标签:factories,factory,python,django 来源: https://codeday.me/bug/20191122/2058219.html