Home > Blockchain >  Django Wagtail dynamically create form without new model
Django Wagtail dynamically create form without new model

Time:01-20

How would I allow my primary user to dynamically create forms they can issue to their end clients. Each of my primary users has their own unique information they would like to collect that I do not know before hand. I would like to avoid creating new models in code for their dynamic needs and then having to migrate the models.

I came across this which had an interesting response but it starts with disclaimer

The flexibility of Python and Django allow developers to dynamically create models to store and access data using Django’s ORM. But you need to be careful if you go down this road, especially if your models are set to change at runtime. This documentation will cover a number of things to consider when making use of runtime dynamic models.

Which leads me to believe a lot can go wrong.

However because I'm using wagtail I believe there is probably a way to use StructBlocks & StreamFields to accomplish it.

Any guidance would be helpful.

CodePudding user response:

I have two possible solutions for you, although it should be said that there is probably some library with Django that I don't know about that does this, but that being said.

  1. Prompt your user for which fields they want and the field type.
  2. Pass this as a dictionary to some function that would generate the HTML code for the form.
  3. When this form is used, instead of worrying about storing the fields seperately, store a dictionary in the Models. There are two ways to do that here

Another way that you could do this, albeit more convoluted but more suited to your needs, is to use MongoDB for the database for Django instead. Because it is unstructured, it might be better suited for your use case. Instructions on using MongoDB for Django are here

CodePudding user response:

Wagtail provides a form builder module for this purpose.

  • Related