From 8d068ef023780dfcfe28964cf9e9f698d27747a7 Mon Sep 17 00:00:00 2001 From: Rob Atkinson <rob@metalinkage.com.au> Date: Wed, 31 Oct 2018 13:28:53 +1100 Subject: [PATCH] Added support for serialiser control in nested fields --- djangoldp/models.py | 3 +++ djangoldp/serializers.py | 13 ++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/djangoldp/models.py b/djangoldp/models.py index 4b9e5a6a..edf293e7 100644 --- a/djangoldp/models.py +++ b/djangoldp/models.py @@ -1,5 +1,8 @@ from django.conf import settings from django.db import models +from django.contrib.auth.models import User + +User._meta.serializer_fields = ('username','first_name','last_name') class LDPSource(models.Model): container = models.URLField() diff --git a/djangoldp/serializers.py b/djangoldp/serializers.py index 673bbb3e..8e471d5e 100644 --- a/djangoldp/serializers.py +++ b/djangoldp/serializers.py @@ -81,7 +81,10 @@ class LDPSerializer(HyperlinkedModelSerializer): serializer_url_field = JsonLdIdentityField def get_default_field_names(self, declared_fields, model_info): - return super().get_default_field_names(declared_fields, model_info) + list(getattr(self.Meta, 'extra_fields', [])) + fields = super().get_default_field_names(declared_fields, model_info) + excludes = list(getattr(self.Meta, 'exclude', [])) + extra = list(getattr(self.Meta, 'extra_fields', [])) + return [f for f in fields if f not in excludes] + extra def to_representation(self, obj): data = super().to_representation(obj) @@ -95,8 +98,12 @@ class LDPSerializer(HyperlinkedModelSerializer): class Meta: model = relation_info.related_model depth = nested_depth - 1 - fields = '__all__' - + try: + fields = model._meta.serializer_fields + except: + fields = '__all__' + + return NestedLDPSerializer, get_nested_relation_kwargs(relation_info) @classmethod -- GitLab