Hi all, in my project I currently manage to collect data with a formset and create a JSON to add to my model. To give a context, each form correspond with a shock (sector + amount fields) and now I generate the JSON putting that data into a dictionary:
{'sector_name1': amount1, 'sector_name2': amount2, 'sector_name3': amount3}
to be stored on the model like this:
shocks = models.JSONField(null=True, blank=True)
Now, I need to create several of those formsets into a higher level key for a year
, so each year will have a dict composed by several shocks generated with my current formset. This is what I want to get from the user input:
{
year1: {'sector_name1': amount1, 'sector_name2': amount2, 'sector_name3': amount3},
year2: {'sector_name1': amount1, 'sector_name2': amount2, 'sector_name3': amount3},
year3: {'sector_name1': amount1, 'sector_name2': amount2, 'sector_name3': amount3}
}
My questions is how is the best way to approach this data collection in a single page form stage. I think of creating a form for year
to store the year_number
and a formset of my shocks, but I am struggling to implement that. As UX, I want to display a first year and allow the user by clicking to add more years with its correspondings shocks forms, and save all in a go. Not using any JS framework. Any ideas of how to get this done?
The relevant part of the code is here (forms, view, template and JavaScript) if anyone wants to take a deeper look.
Many thanks in advance!