dso_api.dynamic_api.serializers module

class dso_api.dynamic_api.serializers.DynamicSerializer(*args, **kwargs)

Bases: FieldAccessMixin, DSOModelSerializer

The logic for dynamically generated serializers.

Most DSO-related logic is found in the base class DSOModelSerializer. This class adds more application-specific logic to the serializer such as:

  • Logic that depends on Amsterdam Schema data.

  • Permissions based on the schema.

  • Temporal relations (ideally in DSO, but highly dependent on schema data).

  • The _links section logic (depends on knowledge of temporal relations).

A part of the serializer construction happens here (the Django model field -> serializer field translation). The remaining part happens in serializer_factory().

dso_api.dynamic_api.serializers.serializer_factory(model: type[schematools.contrib.django.models.DynamicModel], depth: int = 0, nesting_level=0) type[dso_api.dynamic_api.serializers.base.DynamicSerializer]

Generate the DRF serializer class for a specific dataset model.

Internally, this creates all serializer fields based on the metadata from the model, and underlying schema definition. It also generates a secondary serializer for the _links field where all relations are exposed.

In short, this factory does in-memory would you’d typically write as:

class SomeDatasetTableSerializer(DynamicSerializer):
    _links = SomeDatasetTableLinksSerializer(source="*")
    field1 = serializers.CharField(...)
    field2 = serializers.WhateverField(...)

    class Meta:
        model = SomeDatasetTable
        fields = ("_links", "field1", "field2")

…and all child fields are statically defined on the instance whenever possible.

Following DSO/HAL guidelines, objects are serialized with _links and _embedded fields, but without relational fields at the top level. The relational fields appear either in _links or in expanded form in _embedded; depending on the _expand and _expandScope URL parameters.

Parameters
  • model – The dynamic model.

  • depth – Matches the depth parameter for the serializer Meta field. This allows Django Rest Framework to auto-expand relations or omit them.