JSON Serialize

JSON Serialize

2018, Sep 22    

JSON Serialization through ModelSerializer

In django-rest-framework(DRF), first transform original data to convertible data in JSONRender through ModelSerializer. Serializer is similar with Django’s Form, ModelSerializer is Django’s ModelForm. In the role of those, Serializer is just Form which only proceeds POST.

Django Form/ModelForm Django Serializer/ModelSerializer
Specify form field / read from model
Create Form HTML Create JSON serialization
Check validation about input data and acquisition


Define ModelSerializer. It’s really similar to ModelForm.

from rest_framework.serializers import ModelSerializer

# Define ModelSerializer about Post Model
class PostModelSerializer(ModelSerializer):
    class Meta:
        model = Post
        fields = '__all__'


PostModelSerializer supports transformation to dict type for Post model instance. Feed Post instance to PostModelSerializer.

post = Post.objects.first()  # Post type
serializer = PostModelSerializer(post)
serializer.data 


serializer.data type is ReturnDict, which inherited from OrderDict and get the serializer field additionally through generator.

class ReturnDict(OrderedDict):
    def __init__(self, *args, **kwargs):
        self.serializer = kwargs.pop('serializer')
        super().__init__(*args, **kwargs)
        # ...


QuerySet transformation

ModelSerializer supports transformation about QuerySet. “many” parameter of ModelSerializer is False, so we need to set many=True in order to proceed QuerySet.

# QuerySet
serializer = PostModelSerializer(Post.objects.all(), many=True)  

# 지정된 Model Instance 필드를 통해 list/OrderedDict 획득
serializer.data