我正在使用django并且有一个自定义方法来创建某个模型的新对象:
class DiseaseFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Disease
def _create(cls, model_class, *args, **kwargs):
obj = model_class.create_new(first_name=f"Disease{uuid.uuid4()}")
return obj
当我尝试使用DiseaseFactory.create()创建一个DiseaseFactory实例时,它会抛出错误:
TypeError:_create()缺少1个必需的位置参数: ' model_class'
我做错了什么?
答案 0 :(得分:1)
为了使其正常工作,您需要像这样添加@classmethod装饰器:
class DiseaseFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Disease
@classmethod
def _create(cls, model_class, *args, **kwargs):
obj = model_class.create_new(first_name=f"Disease{uuid.uuid4()}")
return obj