Python 如何在子工厂(Django)中包含多对多字段?
我有两种型号:Python 如何在子工厂(Django)中包含多对多字段?,python,django,factory-boy,Python,Django,Factory Boy,我有两种型号:Project和Tag。在Project中有许多字段:Project\u标签我已经为它们编写了工厂。问题是我不知道如何将TagFactory包含到ProjectFactory 我试过: project_tags = factory.SubFactory(TagFactory) 错误: TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use project_t
Project
和Tag
。在Project
中有许多字段:Project\u标签
我已经为它们编写了工厂。问题是我不知道如何将TagFactory
包含到ProjectFactory
我试过:
project_tags = factory.SubFactory(TagFactory)
错误:
TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use project_tags.set() instead
AttributeError: 'ManyToManyDescriptor' object has no attribute 'set'
而且:
Project.project_tags.set()
错误:
TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use project_tags.set() instead
AttributeError: 'ManyToManyDescriptor' object has no attribute 'set'
简单的解决方案是通过或:
类项目工厂(factory.django.DjangoModelFactory):
类元:
模型=项目
tags=factory.RelatedFactoryList(
TagFactory,
尺寸=4,
)
project\u tags=factory.project\u tags.set(此处为TagFactory对象)
我对factory\u boy一点也不熟悉,所以:)@Crosby不工作,但谢谢;)