wagtailstreamforms icon indicating copy to clipboard operation
wagtailstreamforms copied to clipboard

How do you extend the FormSubmission model?

Open johnraz opened this issue 6 years ago • 6 comments

Hello there,

I've been looking into extending the FormSubmission model to add a link to the page the form was submitted from.

Basically I'd like to be able to re-use a form between pages but still be able to know which page the form was submitted from.

My first thought was to add a field to the FormSubmission model and use a submission hook to save the appropriate data but there are two issues there, first the submission hook doesn't seem to give me access to the page object, and next the FormSubmission model seems to be glued to the codebase and it seems very hard to extend it.

First issue can probably be solved by using a custom submission method which gives access to the page.

The second issue is a bit more blurry to me, how would I go about adding this data to the FormSubmission model and make sure that it displays correctly in the admin ?

Should I create a completely new model that I would link to my FormSubmission model and then tweak the views to display it where I need?

Before diving too deep I just wanted your take on this, thanks 😉

johnraz avatar Aug 22 '19 07:08 johnraz

Hi @johnraz, when i first created this i attached the instance of both the page and the user that submitted it to the actual instance of the form, though i have never used it. I thought it may be useful one day. see https://github.com/AccentDesign/wagtailstreamforms/blob/3-dev/wagtailstreamforms/forms.py#L14. So in the submission hooks you will be able to get to form.user and form.page. I have not yet done anything with them or even thought about them. But will this help?

Stu

stuartaccent avatar Aug 23 '19 07:08 stuartaccent

Hey hello @stuartaccent,

Thanks for the answer, that will be super helpful to be able to use submission hooks 👍

I'm still unsure how to extend the model but I'm heading toward a dedicated model that I will simply link with a FK to the related form.

I'll see where that leads me and I'll post the result here.

Thanks again.

johnraz avatar Aug 23 '19 09:08 johnraz

Hi @johnraz

If you would like I could add 2 nullable fks to the submission model and save them when the submission is saved. Would have to be a setnull job so it doesn’t upset the workflow elsewhere of deleting. Form.user can also be anonymous so would have to check is_authenticated. Form.page I believe is always a page. Would prob be best to ensure those props exist at all incase someone is using the app to save other forms in the submission model.

What u think

stuartaccent avatar Aug 23 '19 16:08 stuartaccent

Hey @stuartaccent,

I really needed specific customisation on the model so as I said before, I created a dedicated model with everything I needed (FK to the form, to the page, to the user, a state field and more...) and I create that model instance with a dedicated submission hook.

From there I'm out of streamforms for everything else like displaying the submissions etc.

I'm currently working with a Wagtail modeladmin and custom admin views.

What would be really useful is to be able to extend views and utils provided by streamforms and currently bound to the built-in submission model. eg:

  • An edit submission view: I created a custom view in the admin where I reuse the form submission data to re-display the form in order for the admin user to edit the content of the form if needed.
  • Extend the submission list view: I created a custom list view for my custom submission model where I can filter records by page, would be great to be able to simply re-use the existing submission list view instead.
  • Extend the form block: I tried to extend the form block to add a link to the custom submission list view but so far I can't access the page object and so I can't pre-filter the list which is a bit blocking. I think I'll have to add a custom button to the page in the page explorer to fix this one.

I may have more ideas and needs coming along the way - I'm still iterating a lot on this and I still have quite a few hours budgeted to get it done so I'll see where that leads me.

If anything is of interest for you in the above, I could definitely help bringing back some code from my project to this one.

johnraz avatar Aug 24 '19 07:08 johnraz

He @johnraz we have some development going on in order to support multisite. The need for extending the model(s) is something we currently going to pickup.

We're aiming for the same implementation of the get_image_model & AbstractImage which should be consistent with the core of Wagtail. In that case you would be able to use your own extended model via settings.

Keep you posted.

robmoorman avatar Nov 22 '19 12:11 robmoorman

@johnraz / @robmoorman : I'm interested in amending the Form so rather than saving the form data in a TextField, it's stored in JSONField format. Looking at the code it seems that it is possible to specify an alternative class for the form via WAGTAILSTREAMFORMS_FORM_MODEL. Hence I think it would be possible to create a new derived Form which overrides the get_submission_form() which provides my own Form with the JSONField.

Just wanted to confirm my understanding, as there doesn't appear to be any documentation on this.

I can attempt this locally and if it works, I could update the docs accordingly?

lingster avatar Jun 05 '20 13:06 lingster