Django Rest Framework Approach for JSON API Standards

I am brand new to the Django Rest Framework. I could not find anything in the documents that would allow me to serialize my models in accordance with the JSON API standards ( jsonapi.org ).

Suppose I have the following models.

class Person(models.Model):
    name = models.CharField(max_length=200)


class Car(models.Model):
    owner = models.ForeignKey(Person)
    brand =  
    model = models.CharField(max_length=200)
    plate = models.CharField(max_length=200)

I would like to serialize it so that it provides me with the following output:

{
    "data":[
        {
            "type": "Person",
            "id": 1,
            "attributes": {
                "name": "John",
            },
            "relationships": {
                "cars": [
                    {
                        "data": {
                            "type": "Car",
                            "id": 1,
                            "attributes": {
                                "brand": "Bugatti",
                                "model": "Veyron",
                                "plate": "PAD-305",
                            },
                        },
                    },
                    {
                        "data": {
                            "type": "Car",
                            "id": 2,
                            "attributes": {
                                "brand": "Bugatti",
                                "model": "Chiron",
                                "plate": "MAD-054",
                            },
                        },
                    },
                ],
            },
        },

        {
            "type": "Person",
            "id": 2,
            "attributes": {
                "name": "Charllot",
            },
            "relationships": {
                "cars": [
                    {
                        "data": {
                            "type": "Car",
                            "id": 3,
                            "attributes": {
                                "brand": "Volkswagen",
                                "model": "Passat CC",
                                "plate": "OIJ-210",
                            },
                        },
                    },
                    {
                        "data": {
                            "type": "Car",
                            "id": 4,
                            "attributes": {
                                "brand": "Audi",
                                "model": "A6",
                                "plate": "NAD-004",
                            },
                        },
                    },
                ],
            },
        }
    ],

    "meta":{
        "backend_runtime": "300ms", // processed at the view
    }
}
+4
source share
2 answers

You can create your serializer to return data in any way. For example, if you want to ignore the exact structure of the model, you can do the following

from rest_framework import serializers

class PersonSerializer(serializers.Serializer):
    """
    Person/Car serializer
    """
    id = serializers.IntegerField(read_only=True)
    name = serializers.CharField()
    attributes = serializers.SerializerMethodField()

    def get_attributes(self, obj):
        return {"name": obj.name}

, , , :

from rest_framework import serializers

class CarSerializer(serializers.ModelSerializer):
    """Serializes car object"""    
    class Meta:
        model = Car
        fields = ('id', 'brand',)


class PersonSerializer(serializers.ModelSerializer):
    """Serializes person and car relationship"""
    car = CarSerializer(read_only=True) 

    class Meta:
        model = Person
        fields = ('id', 'name', 'car',)

, ( , ).

+1

Parsers and Renderers - , . , , jsonapi Django REST: https://github.com/django-json-api/django-rest-framework-json-api

Django REST, - .

0

All Articles