[Django]-Viewflow.io: How can I present a custom form for gathering input for a specific task?

6👍

You don’t bound to store all raw data inside the process model. Process model is the root entry for all process related data. Just add ForeignKey to user inside you custom Process model

from django.contrib.auth.models import User
from viewflow.models import Process

class NewUserProcess(Process):
    user = models.ForeignKey(User, blank=True, null=True)

For the forms processing, viewflow doesn’t add anything new to standard django. As usual you can user django standard class based views or plain functional based views, with any form processing code that you need. Here is the example for functional based views:

from viewflow import flow
from viewflow.base import Flow
from viewflow.flow import flow_start_view


class NewUserFlow(Flow):
     registration = flow.StartView(registraton_view).Next(...)


@flow_start_view
def registraton_view(request, activation):
     activation.prepare(request.POST or None, user=request.user)
     form = RegistrationForm(request.POST or None)

     if form.is_valid():
         activation.process.user = form.save()
         activation.done()
         return redirect(...)

     return render(request, 'registration.html', {
        'form': form,
        'activation': activation,
     })

For the case of class based views, you can specify custom form class as usual:

from viewflow.flow.views import StartFlowMixin

class RegistrationView(StartFlowMixin, generic.CreateView):
    form_class = RegistrationForm

    def activation_done(self, form):
        self.activation.process.user = self.object
        self.activation.done()

In addition, you can check viewflow custom views demo source code – https://github.com/viewflow/cookbook/blob/master/custom_views/demo/bloodtest/views.py

Leave a comment