Replies: 2 comments 1 reply
-
Hi @ccurvey, Interesting scenario. Is there a reason why you separated the purchase from the workflow? If they are tightly intertwined, maybe it's better to only have one model. Or is there a reason to duplicate data, that I am missing? In general, I would recommend keeping workflows pretty self-contained (like event-driven programming). Then again, this framework has room for various solutions. In your case, you could also have a follow-up machine task, that updates the value on a related object. It might be more verbose, about the behavior or your workflow and your architecture. Please let me know if you have any further questions or need more suggestions. Cheers! |
Beta Was this translation helpful? Give feedback.
-
a-ha! found it! # A vanilla ModelForm related to the Purchase (the domain object)
class InvoiceForm(forms.ModelForm):
class Meta:
model = Purchase
fields = ['invoice_number']
# Subclass tasks.UpdateView so that we can extend form_valid
class InvoiceView(tasks.UpdateView):
form_class = InvoiceForm
model = Purchase
def form_valid(self, *args, **kwargs):
# the InvoiceView works with the Workflow instance in "object", so we can
# get our Purchase object from that
purchase = self.object.purchase
# there is no "form" attribute/property on the view, so we use "get_form()"
form = self.get_form()
# even though the form has been validated, there is no "cleaned_data" attribute.
# we can either use the "data" attribute, or re-validate
purchase.invoice_number = form.data['invoice_number']
# save our related object, and let the workflow processing continue
purchase.save()
return super().form_valid(*args, **kwargs) |
Beta Was this translation helpful? Give feedback.
-
I'm probably overcomplicating this.
I have a domain model that looks like this:
And I have a workflow that looks like this:
I would like to create a human task that will allow someone to type the invoice number into a form, and then have the invoice number saved on the
Purchase
.The only way I could make this work was to add "invoice_number" to the Workflow, then copy the value over to the Purchase by extending the
form_valid
method, like so:But this feels hacky. Is this the pattern I should be using, or is there a better way?
I was trying to create my own ModelForm with some extra fields, but I can't seem to get a reference to the form from within
form_valid()
Beta Was this translation helpful? Give feedback.
All reactions