Boky
Boky

Reputation: 12084

How to serialize in Django

I have an serializer in Django as follow:

class ListSerializer(serializers.HyperlinkedModelSerializer):
    vehicles = serializers.HyperlinkedRelatedField(
        view_name='asset-detail',
        many=True,
        read_only=True
    )

    class Meta:
        model = List
        fields = ('url', 'name', 'start', 'stop', 'state', 'vehicles')

Result that I get is as follows:

[
    {
        "url": "http://127.0.0.1:8000/sales/api/v1/lists/3741/", 
        "name": "DEA 2017", 
        "start": "2017-03-09T10:00:00", 
        "stop": "2017-12-31T12:00:00", 
        "state": "OPEN", 
        "vehicles": [
            "http://127.0.0.1:8000/sales/api/v1/assets/134299/", 
            "http://127.0.0.1:8000/sales/api/v1/assets/154368/", 
            "http://127.0.0.1:8000/sales/api/v1/assets/154367/", 
            "http://127.0.0.1:8000/sales/api/v1/assets/154246/", 
            "http://127.0.0.1:8000/sales/api/v1/assets/155906/", 
            "http://127.0.0.1:8000/sales/api/v1/assets/155212/", 
            "http://127.0.0.1:8000/sales/api/v1/assets/154574/", 
            "http://127.0.0.1:8000/sales/api/v1/assets/155683/", 
            "http://127.0.0.1:8000/sales/api/v1/assets/153347/", 
            "http://127.0.0.1:8000/sales/api/v1/assets/153183/", 
            "http://127.0.0.1:8000/sales/api/v1/assets/153182/", 
            "http://127.0.0.1:8000/sales/api/v1/assets/153152/", 
            "http://127.0.0.1:8000/sales/api/v1/assets/153116/", 
            "http://127.0.0.1:8000/sales/api/v1/assets/154913/", 
            "http://127.0.0.1:8000/sales/api/v1/assets/152389/", 
            "http://127.0.0.1:8000/sales/api/v1/assets/153459/", 
            "http://127.0.0.1:8000/sales/api/v1/assets/153568/", 
            "http://127.0.0.1:8000/sales/api/v1/assets/153659/"
        ]
    }
]

If I click on one of those url's in vehicles I get an object as follows :

{
    "vin": "WVWZZZ6RZEY104640",
    "make": "VOLKSWAGEN", 
    "model": "POLO", 
    "fuel": "Diesel"
}

What I want is to get result with the objects instead of url's.

Thus something as follows:

[
        {
            "url": "http://127.0.0.1:8000/sales/api/v1/lists/3741/", 
            "name": "DEA 2017", 
            "start": "2017-03-09T10:00:00", 
            "stop": "2017-12-31T12:00:00", 
            "state": "OPEN", 
            "vehicles": [
                {
                   "vin": "WVWZZZ6RZEY104123",
                   "make": "VOLKSWAGEN", 
                   "model": "POLO", 
                   "fuel": "Diesel"
                }, 
                {
                   "vin": "WVWZZZ6RZEY10452",
                   "make": "VOLKSWAGEN", 
                   "model": "Golf", 
                   "fuel": "Diesel"
                }, 
                {...},
                {...},
                {...},
                ....
            ]
        }
    ]

Any advice?

P.S. I'm total beginner and please have mercy :)

Upvotes: 1

Views: 131

Answers (2)

Sijan Bhandari
Sijan Bhandari

Reputation: 3061

I believe you do have a Vehicle model to represent Vehicle attribute. Then you can create serializer for vehicle and use it for you List Serializer as below:

 class VehicleSerializer(serializers.ModelSerializer):
   """Serializer for vehicle"""

      class Meta:
         model = Vehicle



 class ListSerializer(serializers.HyperlinkedModelSerializer):
    vehicles = VehicleSerializer(many=true)

    class Meta:
        model = List
        fields = ('url', 'name', 'start', 'stop', 'state', 'vehicles')

Upvotes: 3

Linovia
Linovia

Reputation: 20996

You are looking for nested serializers which are explained there

Upvotes: 3

Related Questions