📜 ⬆️ ⬇️

Django: JSON edit form widget

The format of data representation in the form of JSON is often found. For example, one of the applications may be the exchange of JSON data when working with API of a third-party service, and in other cases JSON data may be stored within the application model. In some cases, the data needs to be changed and sent back to the remote service, in others it is saved in the model.



If you recall the forms and widgets of forms in Django - powerful tools that simplify the work. And then refer to the source code and official documentation, we will see a list of almost 20 ready-made widgets that are available to us. In addition to official sources, there is a translation into Russian .
Unfortunately, there is nothing ready to be found that would make it easier to work with JSON data in forms. If you turn to search engines, you can find several posts on stackoverflow and private blogs, where solutions are proposed that will be working for some cases. Unfortunately, some examples stop working on data with a complex nested JSON structure, while others require the use of JavaScript for serialization and the reverse process.
')
I tried to find a solution that:

The solution was found in the form of a widget for the form. Here is a demonstration of how a widget builds a form from JSON and after saving it back builds it into JSON:

image

I hope that it will be able to make life easier for someone other than me. Perhaps someone will be able to offer improvements and / or constructive criticism.

Solution and examples posted on Github

Source: https://habr.com/ru/post/180847/


All Articles