From 2faa6c0f61c2b79c9b44c1e8daf381d44dc4a96c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Schl=C3=B6gl?= Date: Wed, 27 Nov 2024 13:45:58 +0100 Subject: [PATCH] fix: dynamically create serializers for relations --- apis_ontology/serializers.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/apis_ontology/serializers.py b/apis_ontology/serializers.py index 6525c12..cd46ef5 100644 --- a/apis_ontology/serializers.py +++ b/apis_ontology/serializers.py @@ -1,6 +1,24 @@ from rest_framework.renderers import serializers from rdflib import Graph, Literal, URIRef, Namespace from rdflib.namespace import RDF, RDFS, XSD +from apis_core.generic.serializers import GenericHyperlinkedModelSerializer +from apis_core.relations.utils import relation_content_types + +# Dynamically create and add serializer classes to this module +for ct in relation_content_types(): + cls = ct.model_class() + if cls.__name__ == "Relation": + continue + serializer_class = type( + f"{cls.__name__}Serializer", + (GenericHyperlinkedModelSerializer,), + { + "__module__": __name__, + "Meta": type("Meta", (), {"model": cls, "fields": "__all__"}), + }, + ) + # Add the new serializer class to the module globals + globals()[f"{cls.__name__}Serializer"] = serializer_class class PersonCidocSerializer(serializers.BaseSerializer):