V
V
Vic Shostak2017-10-19 18:07:56
Django
Vic Shostak, 2017-10-19 18:07:56

Django. How to work with fields (and groups of fields) stored in JSONField()?

Greetings!
Question on Django 1.11 in conjunction with PostgreSQL 9.6: how to work with JSONField type fields ? Let me explain.

  • There is a model with a single field data( JSONField).
  • This field stores all the data from the form fields (from the frontend).
  • The JSON format is approximately with the following logic (the schema is compiled when saved in the view):
{
  'field_name': 'value',
  ...
  'group_fields_name': 
  {
     'field_name_1': 'value',
     'field_name_2': 'value',
  }
  // И так далее. Всего около 20 штук,
  // типы полей — select, input[type=text] и input[type=number]
}

So. Is it possible to work with each field/group (which is saved in JSON format in data) as if these fields were added to the model (edit with value substitution).
This idea was born due to the fact that the frontend form (Vue.js) is replete with all sorts Add more fieldof buttons that add to the DOM from 1 to infinity of fields (nothing is regulated), which at the same time should be editable in the user's personal account (after saving the form) .
Here is one example: https://imgur.com/a/DtUf3 (a group of fields from the listing above group_fields_name).

Another option, "how to do this, not through JSONField()?" - I didn’t find it .. so I’ll be glad for sensible advice and use cases.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question