Django Viewflow 将变量传递给基于函数的视图
Django Viewflow passing variables to function based view
使用基于函数的视图而不是基于 class 的内置视图,使用 viewflow.io 尝试一个非常简单的测试应用程序。预期的想法是添加产品然后批准(通过两种不同的视图/表单)。有两个问题我似乎无法解决:
- 我想将
Product
传递给审批视图(以便进行审批的用户可以看到他们要审批的内容的摘要。我不知道该怎么做 - 我试过了通过 flows.py
中的 flow.View
传递 product_pk
但这会导致错误,如果我将其遗漏,则批准视图会更新所有记录而不是当前产品。
- flows.py 中的
flow.If
门似乎总是 True,无论是否检查了 Product 中的批准字段。理想情况下,我希望批准记录在产品模型而不是过程模型中
可能是我遗漏的超级基本错误/概念 - 任何帮助将不胜感激。
在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 class,URL 与项目 GitHub 页面上的演示应用程序中描述的完全相同。该模板有 {{ activation.management_form }}
标签。
首先,您需要link产品和流程。所以在开始视图中,你可以做
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
使用基于函数的视图而不是基于 class 的内置视图,使用 viewflow.io 尝试一个非常简单的测试应用程序。预期的想法是添加产品然后批准(通过两种不同的视图/表单)。有两个问题我似乎无法解决:
- 我想将
Product
传递给审批视图(以便进行审批的用户可以看到他们要审批的内容的摘要。我不知道该怎么做 - 我试过了通过flows.py
中的flow.View
传递product_pk
但这会导致错误,如果我将其遗漏,则批准视图会更新所有记录而不是当前产品。 - flows.py 中的
flow.If
门似乎总是 True,无论是否检查了 Product 中的批准字段。理想情况下,我希望批准记录在产品模型而不是过程模型中
可能是我遗漏的超级基本错误/概念 - 任何帮助将不胜感激。
在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 class,URL 与项目 GitHub 页面上的演示应用程序中描述的完全相同。该模板有 {{ activation.management_form }}
标签。
首先,您需要link产品和流程。所以在开始视图中,你可以做
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