使用基于函数的视图而不是内置的基于类的视图,使用viewflow.io尝试一个非常简单的测试应用程序。预期的想法是添加产品然后批准(通过两种不同的视图/形式)。我似乎无法解决两个问题:
Product
传递给审批视图(以便执行审批的用户可以看到他们要批准的内容摘要。我不知道如何执行此操作 - 我尝试通过product_pk
通过flow.View
中的flows.py
,但这会导致错误,如果我将其删除,则批准视图会更新所有记录而不是当前产品。flow.If
门始终显示为True。理想情况下,我希望批准记录在产品模型而不是流程模型中我遗漏了可能超级基本的错误/概念 - 任何帮助都会受到赞赏。
在 models.py
中class Product(models.Model):
name = models.CharField(max_length=30)
quantity = models.IntegerField()
approved = models.BooleanField(default=False)
def __str__(self):
return self.name
class ProductProcess(Process):
product = models.ForeignKey(Product, blank=True, null=True)
def approved(self):
return self.product.approved
class ProductTask(Task):
class Meta:
proxy = True
在 flows.py
中class ProductFlow(Flow):
process_cls = ProductProcess
task_cls = ProductTask
start = flow.Start(start_process).Next(this.approve)
approve = flow.View(approve_product).Next(this.checkapproval)
checkapproval = flow.If(cond=lambda p: p.approved()) \
.OnFalse(this.approve) \
.OnTrue(this.end)
end = flow.End()
在 views.py
中@flow_start_view()
def start_process(request, activation):
activation.prepare(request.POST or None,)
form = ProductForm(request.POST or None)
if form.is_valid():
Product.objects.create(
name = form.cleaned_data['name'],
quantity = form.cleaned_data['quantity']
)
activation.done()
return redirect('/test')
return render(request, 'viewflowtest/product.html', {'activation': activation, 'form': form})
@flow_view()
def approve_product(request, activation):
activation.prepare(request.POST or None,)
form = ApproveProductForm(request.POST or None)
if form.is_valid():
Product.objects.update(
approved = form.cleaned_data['approved']
)
activation.done()
return redirect('/test')
return render(request, 'viewflowtest/product.html', {'activation': activation, 'form': form})
调用的表单是一个非常基本的ModelForm类,URL完全与项目GitHub页面上的演示应用程序中描述的一样。该模板具有{{ activation.management_form }}
标记。
答案 0 :(得分:0)
首先,您需要链接产品和流程。所以在开始视图中,你可以做到
if form.is_valid():
product = Product.objects.create(
name = form.cleaned_data['name'],
quantity = form.cleaned_data['quantity']
)
activation.process.product = product
activation.done()
甚至更好,如果ProductForm是ModelForm
if form.is_valid():
product = form.save()
activation.process.product = product
activation.done() # here is new process instance created and saved to db
因此批准视图可以重写为::
@flow_view()
def approve_product(request, activation):
activation.prepare(request.POST or None,)
form = ApproveProductForm(request.POST or None, instance=activation.process.product)
if form.is_valid():
form.save() # here is the approved field is updated
activation.done()
return redirect('/test')
return render(request, 'viewflowtest/product.html', {'activation': activation, 'form': form})
此外,您可以使用基于函数的视图查看viewflow示例 - https://github.com/viewflow/cookbook/blob/master/viewflow_customization/customization/parcel/views.py