calamus icon indicating copy to clipboard operation
calamus copied to clipboard

Serialize nested object by id only

Open Panaetius opened this issue 3 years ago • 2 comments

Currently when serializing with fields.Nested, we always serialize the whole object and add @type information.

In some cases, we want to instead serialize an @id reference instead of the whole nested object.

Theoretically this would be possible with something like fields.Nested(schema.address, AddressSchema, only=("_id",)), but that will result in something like {"@id": "...", "@type": "..."}, whereas for an id reference we wouldn't want to have the @type information included.

We could add an addition flag to fields.Nested to skip adding the @type but that's probably not a clean solution.

A new field like fields.IdReference(schema.address, AddressSchema) would make sense that still takes the child schema like fields.Nested but only returns the @id of the child. On deserializing, it should look up the @id to see if it can get that object in the data and if not, raise an exception.

Panaetius avatar Jan 12 '21 13:01 Panaetius