Solved : The .create() method does not support writable nested fields by default.

Note: This post is related to “Developing REST API using functions in DRF application views” from “Django and Django-REST Framework”

Also check “How to implement writable nested model serializer (Json object in another JSON object) for Django REST Framework”

When we tried to implement following JSON using serializers in Django REST Framework,

[
    {
        "id": 1,
        "userid": "my_userid",
        "username": "lynxbee1",
        "email": "social(at)lynxbee.com",
        "age": "47",
        "useraddress": {
            "id": 1,
            "home_no": "123",
            "street": "my home street",
            "city": "my city",
            "pincode": 123456
        }
    }
]

The related implementation we had did in helloproject/helloapp/serializers.py was as below,

from rest_framework import serializers
from helloproject.helloapp.models import UserInfo, UserAddress

class UserAddressSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserAddress
        fields = ['id', 'home_no', 'street', 'city', 'pincode']

class UserInfoSerializer(serializers.ModelSerializer):
    useraddress = UserAddressSerializer()

    class Meta:
        model = UserInfo
        fields = ['id', 'userid', 'username', 'email', 'age', 'useraddress']

As we can see here, we had implemented two serialisers, “UserAddressSerializer” which formed most inner json object and then UserInfoSerializer which is made using few variables and useraddress combined so it should form the complete JSON as mentioned earlier..

But when we used this nested serialisation, we got an error as below when we tried to http POST to the API,

The .create() method does not support writable nested fields by default.
Write an explicit .create() method for serializer helloproject.helloapp.serializers.UserInfoSerializer, or set read_only=True on nested serializer fields.

The meaning of this error looks quite clear that the nesting of the JSON objects inside another JSON object where we can do http POST is not supported by default.

Solution :

While trying to identify how to fix this, we came accross code from https://github.com/beda-software/drf-writable-nested which allowed us to achieve what we were trying to do, so we did following steps,

pip install drf_writable_nested

and change helloproject/helloapp/serializers.py with following changes,

from drf_writable_nested import WritableNestedModelSerializer

class UserInfoSerializer(WritableNestedModelSerializer, serializers.ModelSerializer):

Notice: we just added “WritableNestedModelSerializer” as a first additional argument to UserInfoSerializer which forms the outer JSON and has combined inner JSON object, UserAddressSerializer, so the final helloproject/helloapp/serializers.py looks like as below,

from rest_framework import serializers
from helloproject.helloapp.models import UserInfo, UserAddress

from drf_writable_nested import WritableNestedModelSerializer

class UserAddressSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserAddress
        fields = ['id', 'home_no', 'street', 'city', 'pincode']

class UserInfoSerializer(WritableNestedModelSerializer, serializers.ModelSerializer):
    useraddress = UserAddressSerializer()

    class Meta:
        model = UserInfo
        fields = ['id', 'userid', 'username', 'email', 'age', 'useraddress']

Leave a Comment