1

trying to serialiaze a request I get from a service, the request is JSON and it is a key/value set that contains sub key/value sets, see example.

The city contains a key named streets that has a dict where key name is the street name that I would of course want to serialize out.

I am quite new to Django rest framework and have no idea how to serialize this as there is limited documentation for this kind of data. I cannot change the incoming request data.

Request:

{
  "cityName": "Some name",
  "population": 5,
  "streets": {
    "theBestStreetName": {
      "streetNumberStart": 1,
      "streetNumberEnd": 200,
      "houses": 5000
    },
    "theWorstStreetName": {
      "streetNumberStart": 1,
      "streetNumberEnd": 7,
      "houses": 4
    }
  }
}

Maybe I need to serialize this manually in some way?

Anders Breid
  • 121
  • 1
  • 8
  • What does your model look like? – 911 May 11 '23 at 06:59
  • I have created a temporary model, but it is not set in stone. I don´t want to create a model(s) looking like the request to say, but rather have an internal data structure. – Anders Breid May 11 '23 at 07:33

1 Answers1

1

Let's go through the provided code line by line to explain its functionality:

from rest_framework import serializers

class CitySerializer(serializers.Serializer):
  cityName = serializers.CharField()
  population = serializers.IntegerField()
  streets = serializers.DictField()

This code imports the necessary modules and defines a serializer class called CitySerializer. It inherits from serializers.Serializer, which is a part of the Django REST Framework (DRF).

def to_representation(self, instance):
representation = super().to_representation(instance)
representation['streets'] = {
    street_name: self._serialize_street(street_data)
    for street_name, street_data in instance.streets.items()
}
return representation
  1. The to_representation method is overridden to provide a custom representation of the serialized object. This method is called when converting the object to a serialized format, such as JSON.
  2. It first calls the parent class's to_representation method to get the initial representation of the object.
  3. Then, it modifies the representation dictionary by replacing the original 'streets' value with a serialized representation of each street.
  4. The instance.streets.items() returns a dictionary view of the streets attribute of the instance, which is iterated over to serialize each street's data.
  5. For each street_name and street_data pair, the _serialize_street method is called to create a serialized representation of the street data.
  6. The resulting dictionary, with serialized streets, is assigned back to the representation ['streets'].

The .py file:

from rest_framework import serializers

class CitySerializer(serializers.Serializer):
     cityName = serializers.CharField()
     population = serializers.IntegerField()
     streets = serializers.DictField()

def to_representation(self, instance):
   representation = super().to_representation(instance)
   representation['streets'] = {
    street_name: self._serialize_street(street_data)
    for street_name, street_data in instance.streets.items()
   }
return representation

def _serialize_street(self, street_data):
return {
    'streetNumberStart': street_data['streetNumberStart'],
    'streetNumberEnd': street_data['streetNumberEnd'],
    'houses': street_data['houses']
}

Json Format:

{
  "cityName": "Some name",
  "population": 5,
  "streets": {
  "theBestStreetName": {
    "streetNumberStart": 1,
    "streetNumberEnd": 200,
    "houses": 5000
    },
  "theWorstStreetName": {
    "streetNumberStart": 1,
    "streetNumberEnd": 7,
    "houses": 4
    }
  }
}

I hope this explanation helps

Zorkan Erkan
  • 1,233
  • 1
  • 5
  • 13