2

This is my json response from Django

{
    id:"123"
    latitude: "37.5111",
    longitude: "126.9743"
}

Want output like this:

"123": {
     latitude: "37.5111",
     longitude: "126.9743"
}

serializers.py

class SearchSerializer(ModelSerializer):

    class Meta:
        model = IpGeo
        fields = [
            # 'id',
            # 'metadata',
            'latitude',
            'longitude'
        ]

views.py

class Search(ListAPIView):

    queryset = IpGeo.objects.all()
    serializer_class = SearchSerializer
0

1 Answer 1

1

serializers.py

from rest_framework import serializers

class SearchSerializer(ModelSerializer):

    custom_field = serializers.SerializerMethodField()


    class Meta:
        model = IpGeo
        fields = [
         'custom,'

        ]
   def get_custom_field(self):
      d = {}
      d[str(self.id)] = dict(latitude=self.latitude,longitude=self.longitude)
      return d
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.