11from django .utils .translation import ugettext_lazy as _
2+ from rest_framework .exceptions import ParseError
23from rest_framework .serializers import *
34
45from rest_framework_json_api .utils import format_relation_name , get_resource_type_from_instance , \
@@ -58,13 +59,60 @@ def __init__(self, *args, **kwargs):
5859 super (SparseFieldsetsMixin , self ).__init__ (* args , ** kwargs )
5960
6061
61- class HyperlinkedModelSerializer (SparseFieldsetsMixin , HyperlinkedModelSerializer ):
62+ class IncludedResourcesValidationMixin (object ):
63+ def __init__ (self , * args , ** kwargs ):
64+ context = kwargs .get ('context' )
65+ request = context .get ('request' ) if context else None
66+ view = context .get ('view' ) if context else None
67+
68+ if request and view :
69+ include_resources_param = request .query_params .get ('include' ) if request else None
70+ if include_resources_param :
71+ included_resources = include_resources_param .split (',' )
72+ for included_field_name in included_resources :
73+ if not hasattr (view , 'included_serializers' ):
74+ raise ParseError ('This endpoint does not support the include parameter' )
75+ if view .included_serializers .get (included_field_name ) is None :
76+ raise ParseError (
77+ 'This endpoint does not support the include parameter for field {}' .format (
78+ included_field_name
79+ )
80+ )
81+ super (IncludedResourcesValidationMixin , self ).__init__ (* args , ** kwargs )
82+
83+
84+ class HyperlinkedModelSerializer (IncludedResourcesValidationMixin , SparseFieldsetsMixin , HyperlinkedModelSerializer ):
6285 """
6386 A type of `ModelSerializer` that uses hyperlinked relationships instead
6487 of primary key relationships. Specifically:
6588
6689 * A 'url' field is included instead of the 'id' field.
6790 * Relationships to other instances are hyperlinks, instead of primary keys.
6891
92+ Included Mixins:
6993 * A mixin class to enable sparse fieldsets is included
94+ * A mixin class to enable validation of included resources is included
7095 """
96+
97+
98+ class ModelSerializer (IncludedResourcesValidationMixin , SparseFieldsetsMixin , ModelSerializer ):
99+ """
100+ A `ModelSerializer` is just a regular `Serializer`, except that:
101+
102+ * A set of default fields are automatically populated.
103+ * A set of default validators are automatically populated.
104+ * Default `.create()` and `.update()` implementations are provided.
105+
106+ The process of automatically determining a set of serializer fields
107+ based on the model fields is reasonably complex, but you almost certainly
108+ don't need to dig into the implementation.
109+
110+ If the `ModelSerializer` class *doesn't* generate the set of fields that
111+ you need you should either declare the extra/differing fields explicitly on
112+ the serializer class, or simply use a `Serializer` class.
113+
114+
115+ Included Mixins:
116+ * A mixin class to enable sparse fieldsets is included
117+ * A mixin class to enable validation of included resources is included
118+ """
0 commit comments