M
M
Meekey2018-08-15 17:59:15
Django
Meekey, 2018-08-15 17:59:15

How to make an array for a JSON field of an object?

There was such a problem. I can pass json format object

[
  {
    "title": "test",
    "address": "55.99752311227003,49.08959250252893"
  },
  {
    "title": "test122",
    "address": "63.08891623673952,46.243883499999946"
  },
  {
    "title": "test1111",
    "address": "55.69684742706125,37.59635133512744"
  },
  {
    "title": "kgeu",
    "address": "55.816852257889856,49.09529045886616"
  }
]

But you need to get a JSON object of the format
[ 
 {
    "title": "kgeu",
    "address": [55.816852257889856,49.09529045886616]
 }
]

setting.py
REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ),
     'DEFAULT_PARSER_CLASSES': (
        'rest_framework.parsers.JSONParser',
    )
}

serializers.py
class FactorySerializer(serializers.ModelSerializer):

    class Meta:
        model = Factory
        fields = ['title', 'address']

views.py
class FactoryListView(generics.ListCreateAPIView):
    queryset = Factory.objects.all()
    serializer_class = FactorySerializer

How to do it?

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