在没有 `self` 的 ~Factory sub-class 中调用 factory_boy super class 方法
Call a factory_boy super class method in a ~Factory sub-class without `self`
有没有办法从子class调用父工厂的方法?
通常的super(ThisClass, self)
或ParentClass.method(self)
方法不起作用,因为self
不是class的实例,它是工厂的对象returns.
class SomethingFactory(factory.DjangoModelFactory):
# Meta, fields, etc
@factory.post_generation
def post(self, create, extracted, **kwargs):
# Some steps
class SomethingElseFactory(SomethingFactory):
@factory.post_generation
def post(self, create, extracted, **kwargs):
super(SomethingElseFactory, self).post(create, extracted, **kwargs)
错误是TypeError: super(type, obj): obj must be an instance or subtype of type
。
(快捷方式 super().post(create, extracted, kwargs)
产生相同的错误。)
如何从子class访问父工厂SomethingFactory.post
方法?
根据您在基础中尝试做的事情 class post()
您能否将其提取到一个函数中并从两个地方调用它:
def set_attributes(obj):
obj.attr1 = True
obj.attr2 = 1000
class SomethingFactory(factory.DjangoModelFactory):
# Meta, fields, etc
@factory.post_generation
def post(self, create, extracted, **kwargs):
set_attributes(self)
class SomethingElseFactory(SomethingFactory):
@factory.post_generation
def post(self, create, extracted, **kwargs):
set_attributes(self)
# Do some other stuff
有没有办法从子class调用父工厂的方法?
通常的super(ThisClass, self)
或ParentClass.method(self)
方法不起作用,因为self
不是class的实例,它是工厂的对象returns.
class SomethingFactory(factory.DjangoModelFactory):
# Meta, fields, etc
@factory.post_generation
def post(self, create, extracted, **kwargs):
# Some steps
class SomethingElseFactory(SomethingFactory):
@factory.post_generation
def post(self, create, extracted, **kwargs):
super(SomethingElseFactory, self).post(create, extracted, **kwargs)
错误是TypeError: super(type, obj): obj must be an instance or subtype of type
。
(快捷方式 super().post(create, extracted, kwargs)
产生相同的错误。)
如何从子class访问父工厂SomethingFactory.post
方法?
根据您在基础中尝试做的事情 class post()
您能否将其提取到一个函数中并从两个地方调用它:
def set_attributes(obj):
obj.attr1 = True
obj.attr2 = 1000
class SomethingFactory(factory.DjangoModelFactory):
# Meta, fields, etc
@factory.post_generation
def post(self, create, extracted, **kwargs):
set_attributes(self)
class SomethingElseFactory(SomethingFactory):
@factory.post_generation
def post(self, create, extracted, **kwargs):
set_attributes(self)
# Do some other stuff