1+ import collections
12import json
23
3- from rest_framework .fields import MISSING_ERROR_MESSAGE
4+ from rest_framework .fields import MISSING_ERROR_MESSAGE , SerializerMethodField
45from rest_framework .relations import *
56from django .utils .translation import ugettext_lazy as _
67from django .db .models .query import QuerySet
1011 get_resource_type_from_queryset , get_resource_type_from_instance , \
1112 get_included_serializers , get_resource_type_from_serializer
1213
14+ LINKS_PARAMS = ['self_link_view_name' , 'related_link_view_name' , 'related_link_lookup_field' , 'related_link_url_kwarg' ]
15+
1316
1417class ResourceRelatedField (PrimaryKeyRelatedField ):
1518 self_link_view_name = None
@@ -189,17 +192,20 @@ def __init__(self, child_relation=None, *args, **kwargs):
189192 # DRF 3.1 doesn't expect the `many` kwarg
190193 kwargs .pop ('many' , None )
191194 model = kwargs .pop ('model' , None )
195+ if child_relation is not None :
196+ self .child_relation = child_relation
192197 if model :
193198 self .model = model
194- super (SerializerMethodResourceRelatedField , self ).__init__ (child_relation , * args , ** kwargs )
199+ super (SerializerMethodResourceRelatedField , self ).__init__ (* args , ** kwargs )
195200
196201 @classmethod
197202 def many_init (cls , * args , ** kwargs ):
198- list_kwargs = {'child_relation' : cls (* args , ** kwargs )}
203+ list_kwargs = {k : kwargs .pop (k ) for k in LINKS_PARAMS if k in kwargs }
204+ list_kwargs ['child_relation' ] = cls (* args , ** kwargs )
199205 for key in kwargs .keys ():
200206 if key in ('model' ,) + MANY_RELATION_KWARGS :
201207 list_kwargs [key ] = kwargs [key ]
202- return SerializerMethodResourceRelatedField (** list_kwargs )
208+ return cls (** list_kwargs )
203209
204210 def get_attribute (self , instance ):
205211 # check for a source fn defined on the serializer instead of the model
@@ -210,10 +216,7 @@ def get_attribute(self, instance):
210216 return super (SerializerMethodResourceRelatedField , self ).get_attribute (instance )
211217
212218 def to_representation (self , value ):
213- if isinstance (value , list ):
219+ if isinstance (value , collections . Iterable ):
214220 base = super (SerializerMethodResourceRelatedField , self )
215221 return [base .to_representation (x ) for x in value ]
216- return super (SerializerMethodResourceRelatedField , self ).to_representation (value )
217-
218- def get_links (self , obj = None , lookup_field = 'pk' ):
219- return OrderedDict ()
222+ return super (SerializerMethodResourceRelatedField , self ).to_representation (value )
0 commit comments