带有 django viewflow 前端的内联表单集

Inline formsets with django viewflow frontend

我可以获得内联表单集以与 django 一起正常工作,但是当尝试使用 viewflow 和内置的工作流前端时,我似乎无法在工作流中使用内联表单。

引用表单集的正确方法是什么?

由于工作流模型继承自 viewflow.models.Process 对象,我似乎无法理解如何创建内联相关模型。

用例:我有一个包含大约 50 个不同字段的工作流,我可以 运行 使用 Flow 并将流程分解为大约 10 个步骤的逻辑部分的工作流。但是,在流程启动并输入前 3 或 4 个字段后,我希望第 2 步成为一个内联表单,其中为该特定工作流事件的用户分配了各种角色(未知数量)。

我已经有一个单独的 table,在那里他们被手动分配给角色,我可以让 Flow 将下一个任务分配给分配给那个 "steps" 角色的任何人,它工作正常,但是我希望分配角色的操作成为实际工作流程本身的一部分。

我怀疑我应该将内联表单集添加为父模型(进程)的一个字段,但看不出如何...

我接近(至少在外观上)下面的代码但是

  1. 我没有得到重复的内嵌表单
  2. 当我提交时出现错误 ManagementForm 数据丢失或被篡改
# models.py

from django.db import models
from django.utils import timezone
from filer.fields.file import FilerFileField
from viewflow.models import Process

# Create your models here.

class RoutingSheetProcess(Process):
    project     = models.CharField(verbose_name=u"Project#",max_length=20,blank=False,null=False)
    internaltask     = models.CharField(verbose_name=u"Task #",max_length=20,blank=False,null=False)
    pono        = models.CharField(verbose_name=u"P.O. Number",max_length=20,blank=True,null=True)
    sampletype  = models.CharField(verbose_name=u"Sample Type",max_length=10)
    sampleother = models.CharField(verbose_name=u"Other Sample Type",max_length=20,blank=True,null=True)
    sampledate  = models.DateField(verbose_name=u"Date Sample Taken",blank=True,null=True)

class InlineModel(models.Model):
    routingsheet = models.ForeignKey(RoutingSheetProcess,on_delete=models.CASCADE,blank=False,null=False)
    field1 = models.CharField(max_length=20,blank=True,null=True)
    field2 = models.CharField(max_length=20,blank=True,null=True)
    field3 = models.CharField(max_length=20,blank=True,null=True)
# flows.py

from viewflow import flow, frontend
from viewflow.base import this, Flow
from viewflow.flow.views import CreateProcessView, UpdateProcessView

from .models import RoutingSheetProcess
from . import views



@frontend.register
class RoutingSheetFlow(Flow):
    process_class = RoutingSheetProcess

    start = (
        flow.Start(
            CreateProcessView,
            fields=["project","internaltask","pono"]
        ).Permission(
            auto_create=True
        ).Next(this.sample)
    )

    sample = (
        flow.View(
            UpdateProcessView,
            fields=["sampletype","sampleother","sampledate"]
        ).Permission(
            auto_create=True
        ).Next(this.inline_sample)
    )

    inline_sample = (
        flow.View(
            views.InlineSample
        ).Permission(
            auto_create=True
        ).Next(this.end)
    )

    end = flow.End()
# views.py

from django.shortcuts import render

from viewflow.flow.views import CreateProcessView, UpdateProcessView

# Create your views here.

from .forms import *

from material import *

class InlineSample(UpdateProcessView):
    form_class=InlineForm

    def get_context_data(self, **kwargs):
        data = super(InlineSample, self).get_context_data(**kwargs)
        if self.request.POST:
            data['fields'] = InlineFormset(self.request.POST)
        else:
            data['fields'] = InlineFormset()
        #print(data)
        #for f in data['fields']:
        #    print(f)
        return data


    layout = Layout(
        Fieldset('Inline Fields',
            Row('field1','field2','field3'),
        )
    )

    def get_object(self):
        #print(self.activation.process)
        return self.activation.process 
# forms.py

from django import forms
from django.forms import ModelForm
from django.forms.models import inlineformset_factory, formset_factory

from .models import *

InlineFormset=inlineformset_factory(
        RoutingSheetProcess, #parent
        InlineModel, #child
        fields=('field1','field2','field3'), #fieldlist from child
        extra=2
        )  

class InlineForm(ModelForm):

    class Meta:
        model=InlineModel

        exclude=[]
        widgets = {
            #'description':forms.Textarea,
        }

在流程的第 2 步生成此... output of step 2

为了处理表单集,django-material 提供了 InlineFormSet 字段,但它仅在 PRO 版本中可用

http://docs.viewflow.io/forms_formsets.html https://github.com/viewflow/viewflow/blob/master/demo/shipment/forms.py#L7