diff --git a/ifbcat_api/renderers.py b/ifbcat_api/renderers.py index da72d80..95e269a 100644 --- a/ifbcat_api/renderers.py +++ b/ifbcat_api/renderers.py @@ -12,7 +12,10 @@ ManyToManyRel, ForeignKey, ) -from django.db.models.fields.related_descriptors import ReverseManyToOneDescriptor +from django.db.models.fields.related_descriptors import ( + ReverseManyToOneDescriptor, + ManyToManyDescriptor, +) from django.urls import reverse from rdflib import ConjunctiveGraph, URIRef, Namespace, Literal, BNode from rdflib.namespace import RDF, XSD @@ -110,18 +113,28 @@ def get_rdf_mapping(instance_id=None): if attr_name[0] == '_': continue if type(mapping) == dict and (sub_fields := mapping.get('_fields', None)) is not None: - sub_node = BNode() - G.add((sub_node, RDF.type, getattr(SCHEMA, mapping["_type"]))) - for attr_name, schema_attr in sub_fields.items(): - G.add( - ( - sub_node, - getattr(SCHEMA, schema_attr), - Literal(getattr(model.objects.get(id=item['id']), attr_name)), + # by default, we use the object itself as source for the sub node + sub_nodes_src = model.objects.filter(id=item['id']) + try: + if type(getattr(model, attr_name)) in (ManyToManyDescriptor,): + # if the attr_name exists and is an M2M, we use them as sources of the sub nodes + sub_nodes_src = getattr(model.objects.get(id=item['id']), attr_name).all() + except AttributeError: + pass + for sub_node_src in sub_nodes_src: + # for each source, create a new sub node, and get info from this source + sub_node = BNode() + G.add((sub_node, RDF.type, getattr(SCHEMA, mapping["_type"]))) + for sub_attr_name, schema_attr in sub_fields.items(): + G.add( + ( + sub_node, + getattr(SCHEMA, schema_attr), + Literal(getattr(sub_node_src, sub_attr_name)), + ) ) - ) - G.add((object_uri, getattr(SCHEMA, mapping['schema_attr']), sub_node)) + G.add((object_uri, getattr(SCHEMA, mapping['schema_attr']), sub_node)) continue try: # get the value from the serialized object diff --git a/ifbcat_api/serializers.py b/ifbcat_api/serializers.py index a97ee48..f5dc0b5 100644 --- a/ifbcat_api/serializers.py +++ b/ifbcat_api/serializers.py @@ -396,7 +396,14 @@ class Meta: costs=dict(schema_attr='offers', _type='Demand'), homepage='url', maxParticipants='maximumAttendeeCapacity', - organisedByOrganisations='organizer', + organisedByOrganisations=dict( + schema_attr='organizer', + _type="Organization", + _fields=dict( + name='name', + homepage='url', + ), + ), organisedByTeams=dict( schema_attr='organizer', _type="Organization",