Skip to content

Commit

Permalink
fix: dynamically create serializers for relations
Browse files Browse the repository at this point in the history
  • Loading branch information
sennierer committed Nov 27, 2024
1 parent dc1ef5f commit 344d58a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions apis_ontology/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
from rest_framework.renderers import serializers
import apis_ontology.models as ontology_models
from rdflib import Graph, Literal, URIRef, Namespace
from rdflib.namespace import RDF, RDFS, XSD
from apis_core.generic.serializers import GenericHyperlinkedModelSerializer

# get classes for all relation models
relation_classes = [
cls
for name, cls in ontology_models.__dict__.items()
if isinstance(cls, type)
and issubclass(cls, ontology_models.Relation)
and not name == "Relation"
]

# Dynamically create and add serializer classes to this module
for cls in relation_classes:
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):
Expand Down

0 comments on commit 344d58a

Please sign in to comment.