我正在使用 django-viewflow 来跟踪复杂的业务流程。为了避免有长的 Flow 类和 flow.py 文件,我希望将一个流馈送到另一个流。这可能吗?
我尝试了以下代码,但 Python 抛出了 NotImplemented 异常。
class SecondFlow(Flow):
process_class = SecondProcess
start = (...)
class FirstFlow(Flow):
process_class = FirstProcess
start = (
flow.Start(
CreateProcessView,
fields=['foo']
).Next(SecondFlow.start)
)
如果 FirstFlow 路由到 SecondFlow 的开头,那就太好了。
编辑:我尝试使用提供的建议和文档,但出现以下错误:'StartFunction' object has no attribute 'prepare'
下面是我的新代码。
from viewflow import flow, frontend
from viewflow.base import this, Flow
from viewflow.flow.views import CreateProcessView, UpdateProcessView
from .models import FirstProcess, SecondProcess
@frontend.register
class SecondFlow(Flow):
process_class = SecondProcess
start = flow.StartFunction(this.create_flow
).Next(this.enter_text)
def create_flow(self, activation, **kwargs):
activation.prepare()
activation.done()
enter_text = (
flow.View(
UpdateProcessView,
fields=['text']
).Next(this.end)
)
end = flow.End()
@frontend.register
class FirstFlow(Flow):
process_class = FirstProcess
start = (
flow.Start(
CreateProcessView,
fields=['text']
).Next(this.initiate_second_flow)
)
initiate_second_flow = (
flow.Handler(this.start_second_flow
).Next(this.end)
)
def start_second_flow(self, activation):
SecondFlow.start.run()
end = flow.End()
第二次编辑:在我将装饰器添加到create_flow
SecondFlow 的方法后它可以工作。
from django.utils.decorators import method_decorator
...
@frontend.register
class SecondFlow(Flow):
process_class = SecondProcess
start = flow.StartFunction(this.create_flow
).Next(this.enter_text)
@method_decorator(flow.flow_start_func)
def create_flow(self, activation, **kwargs):
activation.prepare()
activation.done()
...