2

使用基于函数的视图而不是基于内置类的视图尝试使用viewflow.io的非常简单的测试应用程序。预期的想法是添加产品然后批准(通过两个不同的视图/表单)。有两个问题我似乎无法解决:

  1. 我想将 传递Product给批准视图(以便进行批准的用户可以看到他们要批准的内容的摘要。我不知道该怎么做 - 我尝试通过product_pkinflow.View但这flows.py会导致错误,如果我忽略它,那么批准视图会更新所有记录而不是当前产品。
  2. flow.If无论 Product 中的approved 字段是否已被检查,flows.py 中的门似乎总是为True。理想情况下,我希望批准记录在产品模型而不是流程模型中

可能是我缺少的超级基本错误/概念 - 任何帮助将不胜感激。

模型.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

流.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 }}标签。

4

1 回答 1

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})

此外,您可以查看带有基于函数的视图的视图流示例 - https://github.com/viewflow/cookbook/blob/master/viewflow_customization/customization/parcel/views.py

于 2016-03-24T11:46:29.743 回答