1
1
1thater2020-05-26 20:19:02
Django
1thater, 2020-05-26 20:19:02

How can I write a model in django so that I can work comfortably with JSON?

I asked the same last question, but I didn’t get a clear answer for me, I’m creating a new one, please don’t swear.

What I need is that:
the site makes a request to the server -> the server looks for certain data and returns JSON -> then JS works on the client with this JSON.

Data is added through the admin panel, for example:
Type - a regular point;
then a certain structure of nested fields so that such an "element" comes out of them:

{
    "endPlace": [425, 330],
    "icon": "endIcon30",
    "startPlace": [
        {
            "startPoint": [45, 875],
            "icon": "startIcon20",
            "url": ["[email protected]", "3mcWTjoi2zFE"]
        },
        {
            "startPoint": [51, 670],
            "icon": "startIcon20",
            "url": ["3mcW212zFE"]
        },
        {
            "startPoint": [51, 670],
            "icon": "startIcon20",
            "url": ["3mcW212zFE"]
        }
    ]
}

,
2. Then they are written to the database.

And when the server receives a request, it will collect a JSON file, from the "elements above" by type, and the output will be:
JSON of this nature:
[{
    "endPlace": [425, 330],
    "icon": "endIcon30",
    "startPlace": [
        {
            "startPoint": [45, 875],
            "icon": "startIcon20",
            "url": ["[email protected]", "3mcWTjoi2zFE"]
        },
        {
            "startPoint": [51, 670],
            "icon": "startIcon20",
            "url": ["3mcW212zFE"]
        },
        {
            "startPoint": [51, 670],
            "icon": "startIcon20",
            "url": ["3mcW212zFE"]
        }
    ]
},
{
  "endPlace": [452, 175],
  "icon": "endIcon30",
  "startPlace": [
      {
          "startPoint": [936, 845],
          "icon": "startIcon20",
          "url": ["[email protected]", "[email protected]"]
      }
  ]
},
{
  "endPlace": [10, 10],
  "icon": "endIcon30",
  "startPlace": [
      {
          "startPoint": [54, 845],
          "icon": "startIcon20",
          "url": ["_aC5423TjUU", "3mcWT345zFE"]
      },
      {
          "startPoint": [54, 845],
          "icon": "startIcon20",
          "url": ["_aC5423TjUU", "3mcWT345zFE"]
      }
  ]
}]


I really hope for a detailed explanation, an indication of materials that will help implement this.
Maybe someone made models that created this.

If something is not clear described, I will be happy to describe in other words.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2020-05-27
@1thater

Well, in your case, everything is quite simple. But you need to use Postgres as a database, since in this case you can use the ArrayField field in Django.

class StartPlace(models.Model):

  start_point = models.ArrayField(max_length=10)
  icon = models.CharField(max_length=50)
  url = models.ArrayField(max_length=10)

class Point(models.Model):

  end_place = models.ArrayField(max_length=10)
  icon = models.CharField(max_length=50)
  start_place = models.ManyToManyField(StartPlace)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question