This one is interesting. I was also looking for the solution of the same problem.
If I understand correctly, the problem with forms is -
User can choose the name of the field and the type of the fields e.g. IntegerField(). FloatField(), etc. Kind of a form that is generated using a form :P
More like a Form Builder App which will help users create DYNAMIC FORMS.
Am I correct?
We could create a form for that with 2 inputs elements.
One input to enter the name of the field.
Second would be a dropdown (select html element) with the type of the model fields that django models.Model has. We can also add select dropdown for constraints.
Store the values of these two in a JSONField(). This part is simple and possible.
The real challenge is -
How to turn them into models and store the actual data?
Solution 1
That is also simple. We could create a default string that resembles the source code of a model class of a django model.
and create the code for it by writing the if clauses for diff fields of the models and create a string for the same. Then write this string to a .py file.
e.g.
"class BaseModel(models.Model):
created = models.DateTimeField(auto_now=False, auto_now_add=True,)
updated = models.DateTimeField(auto_now=True, auto_now_add=False,)"
The Problem with this approach is app will end up with lots of .py files that has diff models.
Making migrations and Migrating will also be a pain point.
Making any migrations will require stopping and restarting the server (nginx+gunicorn), that is not possible in a production environment.
Will have to restart the server for every new form created and migrated.
Not a good idea to do this.
Solution 2
As each form/model would have a separate schema, using a JSONField() makes sense here.
As we are storing forms in JSONField(), store the data also in the JSONField().
This wont require making any models/migrations and migrating the database. Also server restarts are not required at all.
This is more feasible solution and will be easier approach than the earlier one.
However, making the forms populate/reder dynamically from the stored json field and validating them dynamically according to their schema NEEDS SOME R&D.
Give it a try and share the results with me.
I hope it was worth your time :)