Skip to content

id field set on ModelSerializer is bypassed and not used #323

Closed
@erichaus

Description

@erichaus

We noticed that the id = serializers.SerializerMethodField() that we have set on our ModelSerializer is not being used when this package creates the json-api structure. Upon investigating I saw this line which appears to use the resource_object.pk for the id field:

https://github.com/django-json-api/django-rest-framework-json-api/blob/develop/rest_framework_json_api/renderers.py#L387

class FooSerializer(ModelSerializer):
    id = serializers.SerializerMethodField()

    def get_id(self, obj):
        """
        Returns the Foo.short_id as `id` for the client.
        """
        return shortuuid.serialize_uuid(obj.id).decode('utf-8')

This causes a bug where if someone has written a custom serialized id field it is bypassed completely. In our case we are customizing the id of our model for the client.

We may create a shortcut locally but I think this package should respect anything defined in the local serializers and use that rather than bypassing it. It also brings up a question on using this package in general: does this package change the way fields must be defined compared to the typical DRF way? Does it automatically translate the normal DRF serializer fields into the appropriate json-api versions? I realize it's somewhat complex so just trying to gauge how this will affect writing serializers for our project

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions