Skip to content
Snippets Groups Projects
Commit c1823ca6 authored by Sylvain Le Bon's avatar Sylvain Le Bon
Browse files

update: configurable fields (fix #6)

parent d3c4d19a
No related branches found
No related tags found
No related merge requests found
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import get_resolver
from django.utils.datastructures import MultiValueDictKeyError
from rest_framework.relations import HyperlinkedRelatedField, ManyRelatedField
......@@ -22,7 +23,10 @@ class JsonLdRelatedField(HyperlinkedRelatedField):
except MultiValueDictKeyError:
pass
def to_representation(self, value):
return {'@id': super().to_representation(value)}
try:
return {'@id': super().to_representation(value)}
except ImproperlyConfigured:
return value.pk
class LDPSerializer(HyperlinkedModelSerializer):
url_field_name = "@id"
......
......@@ -28,14 +28,20 @@ class NoCSRFAuthentication(SessionAuthentication):
class LDPViewSet(ModelViewSet):
model = None
fields = None
renderer_classes = (JSONLDRenderer, )
parser_classes = (JSONLDParser, )
authentication_classes = (NoCSRFAuthentication,)
def __init__(self, **kwargs):
super().__init__(**kwargs)
class_attrs = {'Meta': type('Meta', (), {'model': self.model, 'exclude': ()})}
self.serializer_class = type(LDPSerializer)(self.model._meta.object_name.lower()+'Serializer', (LDPSerializer,), class_attrs)
meta_args = {'model': self.model}
if self.fields:
meta_args['fields'] = self.fields
else:
meta_args['exclude'] = ()
meta_class = type('Meta', (), meta_args)
self.serializer_class = type(LDPSerializer)(self.model._meta.object_name.lower()+'Serializer', (LDPSerializer,), {'Meta': meta_class})
def get_queryset(self, *args, **kwargs):
if self.model:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment