Custom primary key serialization. by ovangle · Pull Request #2789 · encode/django-rest-framework (original) (raw)
This PR has little to do with #2788 #3000.
Currently, the code in question assumes that the primary key's value is a primitive and passes it on to the json/xml/etc serializer to serialize. In the case of UUID's, this just calls str
on the result, writing a 'verbose hex' format UUID in the serialized representation.
This may not be the desired behaviour, so I've changed the foreign key field to accept an optional subfield, which can be used to customise the output representation of the foreign key's value.
But it can be used to customise the output serialization of primary keys in general.
ps. the implementation of this should have been independent to the implementation of #2788, but the unit tests rely on being able to serialise a UUID to a format other than the verbose hex format.