Search code examples
djangoapidjango-rest-frameworkdjango-serializer

How to use a nested serializer dynamically for two serializers?


I am working on an API, with Django, Django Rest Framework, and trying to achieve these(ad described)

First Serializer

class DeviceConfigSerializer(serializers.ModelSerializer):
    config = serializers.JSONField(initial={})
    context = serializers.JSONField(initial={})
    templates = FilterTemplatesByOrganization(many=True)

    class Meta:
        model = Config
        fields = ['backend', 'status', 'templates', 'context', 'config']
        extra_kwargs = {'status': {'read_only': True}}

Now I have two nested serializer containing the above serializer for the LIST and DETAIL endpoints:-

Second Serializer

class DeviceListSerializer(FilterSerializerByOrgManaged, serializers.ModelSerializer):
    config = DeviceConfigSerializer(write_only=True, required=False)

    class Meta(BaseMeta):
        model = Device
        fields = ['id','name','organization','mac_address','key','last_ip','management_ip', 
                  'model', 'os', 'system', 'notes', 'config', 'created', 'modified',]

Third Serializer

class DeviceDetailSerializer(BaseSerializer):
    config = DeviceConfigSerializer(allow_null=True)

    class Meta(BaseMeta):
        model = Device
        fields = ['id','name','organization','mac_address','key','last_ip','management_ip',
                  'model','os','system','notes','config','created','modified',]

Now, I am using the same DeviceConfigSerializer serializer for List, and Detail endpoint, but for the list endpoint I have set the nested serializer as write_only=True, But What I am trying to do with the list endpoint that is DeviceListSerializer serilaizer is that out of all the fields from the DeviceConfigSerializer, I want the status, and backend fields to be both read & write and others fields as write_only.

Presently with this configuration I am getting the response from the DeviceListSerializer as this:-

{
            "id": "12",
            "name": "tests",
            "organization": "-------",
            "mac_address": "-------",
            "key": "------",
            "last_ip": null,
            "management_ip": null,
            "model": "",
            "os": "",
            "system": "",
            "notes": "",
            "created": "2021-04-26T10:41:25.399160+02:00",
            "modified": "2021-04-26T10:41:25.399160+02:00"
}

What I am trying to achieve is:-

{
            "id": "12",
            "name": "tests",
            "organization": "----",
            "mac_address": "-----",
            "key": "----",
            "last_ip": null,
            "management_ip": null,
            "model": "",
            "os": "",
            "system": "",
            "notes": "",
            "config": {
                 "status": "...",
                 "backend": "...",
            ,}
            "created": "2021-04-26T10:41:25.399160+02:00",
            "modified": "2021-04-26T10:41:25.399160+02:00"
}

PS: I tried by introducing an extra serializer for this two fields and nest it to the DeviceListSerializer, but I don't want to introduce an extra serializer for this two fields, and looking forward if this could be achieved with the same nested serializer.

  • Every Device instance contains config.

In short:-

I am trying to use the same DeviceConfigSerializer, for both DeviceListSerilaizer, and DeviceDetailSerializer. But for the DeviceListSerializer I want the status, & backend field from the DeviceConfigSerializerto be both read & write, which is presenty set to write only for the DeviceListSerializer.


Solution

  • This is not possible, so I had to introduce two new serializers for my need.