4

I'm quite new to Django Rest Framework. I couldn't find on the docs something that would allow me to serialize my models according to the JSON API standards (jsonapi.org).

Let's 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 in a way that it would provide 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
    }
}
3
  • im pretty sure you will have to convert the model manually to that form Commented May 10, 2016 at 22:46
  • I think that would violate the ORM principles. I shouldn't have to design my models to represent it in a specific JSON structure. I wonder what are the DRF tools for represent it in a flexible way. Commented May 10, 2016 at 22:50
  • 1
    I guess this github.com/django-json-api ... Commented May 10, 2016 at 22:53

2 Answers 2

2

You can create your serializer to return the data any way you want. For example, if you want to ignore the exact model structure 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}

If you want a serializer structure that is closer to your models, you can relate model serializers using the following approach:

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',)

In both cases, you would be passing a queryset to the serializer that contains these fields (and in the nested model serializer, the existing relationships).

Sign up to request clarification or add additional context in comments.

Comments

1

Parsers and Renderers are the component that allows you to alter the serializer's response. Note that there's already a third party that implements jsonapi with Django REST framework: https://github.com/django-json-api/django-rest-framework-json-api

Dont forget to look at the Django REST framework third party page if you're looking for something.

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.