[英]Django Viewflow passing variables to function based view
Trying out a very simple test app with viewflow.io using function based views rather than the built-in class based views. 使用基于功能的视图而不是基于内置类的视图,通过viewflow.io试用一个非常简单的测试应用。 The intended idea is that a product is added and then approved (via two different views/ forms).
预期的想法是先添加产品,然后再批准(通过两种不同的视图/形式)。 There are two issues I cannot seem to work out:
我似乎无法解决两个问题:
Product
to the approval view (so that the user doing the approval can see the summary of what they are meant to approve. I am not sure how to do this - I tried passing the product_pk
via the flow.View
in flows.py
but this results in an error and if I leave it out then the approval view updates all records rather than the current product. Product
传递给批准视图(以便进行批准的用户可以看到他们要批准的摘要。我不确定如何执行此操作-我尝试通过flow.View
传递product_pk
。 flows.py
但这会导致错误,如果我将其遗漏,则批准视图会更新所有记录,而不是当前产品。 flow.If
gate in flows.py always seems to be True regardless of whether the approved field in Product has been check or not. flow.If
门始终似乎为True。 Ideally I am hoping that the approval is recorded in the Product model rather than the process model Probably super basic mistake/ concept I am missing - any help would be appreciated. 我可能缺少超级基本错误/概念-我们将不胜感激。
In models.py 在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
In flows.py 在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()
In views.py 在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})
The form that is called is a very basic ModelForm class and the URLs are exactly as is described in the demo applications on the project GitHub pages. 调用的表单是一个非常基本的ModelForm类,URL完全与项目GitHub页面上的演示应用程序中所述的相同。 The template has the
{{ activation.management_form }}
tag. 该模板具有
{{ activation.management_form }}
标签。
First of all, you need to link the product and process. 首先,您需要链接产品和过程。 So in start view, you can do
因此,在开始视图中,您可以
if form.is_valid():
product = Product.objects.create(
name = form.cleaned_data['name'],
quantity = form.cleaned_data['quantity']
)
activation.process.product = product
activation.done()
or even better, if the ProductForm is the ModelForm 甚至更好,如果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
So the approval view could be rewritten as:: 因此,批准视图可以重写为:
@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})
In addition, you can take a look to the viewflow example with the function-based views - https://github.com/viewflow/cookbook/blob/master/viewflow_customization/customization/parcel/views.py 此外,您可以查看基于函数的视图的viewflow示例-https: //github.com/viewflow/cookbook/blob/master/viewflow_customization/customization/parcel/views.py
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.