In my project I have views that have about 6 formsets, some even with nested formsets (adaptations were made for this). I sometimes feel that this whole construction process becomes tiring and difficult to maintain.
Sometimes I think about using the rest_framework with drf-writable-nested for views with all this complexity of forms and formset. It seems to be simpler to send a json structure to nested fields, am I correct?
Thinking about django’s default form and formsets structure, I’m curious to know if there is an alternative way to work with this.
Here’s an example of the tangle of things a formset can have.
I agree - it can. Unfortunately, that’s the complexity of working with HTML as the fundamental UI.
It might be simpler - but you’re just shifting the work. You’re moving the work from Django to your front end. If you’re going to provide the same degree of robustness and security as Django provides, you’re still going to need to do all the work done by formsets - and if you’re managing those forms in the browser, you’re doing the work twice. (You can never rely upon browser validation only. Everything submitted from a browser must be validated in the server as well.)
There are always alternatives. You can rely more upon formsets built on the fly and retrieved by the browser via AJAX calls. That’s a technique we use frequently.
The page formset might have an icon for each row for a detailed subordinate formset. Clicking on that icon causes an AJAX call to be made to retrieve a formset to be presented in a modal dialog box. That dialog is just a standard formset. When it’s submitted, it’s submitted to a view that handles just that formset.
It would be easy for us to extend that structure in a couple different ways - any of which would not affect the existing structure.
This is interesting…
It even came to my mind to use htmx to submit these parts.
I would just need to think about the validations that involve the parent form. Maybe send via ajax all the data used so far?