def _get_ref_params(self, instance, loc, top_spec, automated,
field_kwargs):
"""
Get extra params needed to initialize a
`serializers.HyperlinkedIdentityField`.
"""
ref_kwargs = instance['.ref']
many = ref_kwargs.get('many', False)
ref = ref_kwargs['to']
endpoint, collection = tuple(ref.split('/'))
extra = {'view_name': '%s-detail' % (endpoint + '_' + collection)}
if not automated:
extra['many'] = many
if not field_kwargs.get('read_only'):
# In case it is not a read only field, specify its queryset.
ref_model = self._get_or_import_model(
endpoint + '/' + collection,
loc[:1] + ('.drf_collection', 'model'), top_spec)
extra['queryset'] = ref_model.objects.all()
return extra
python类HyperlinkedIdentityField()的实例源码
def _get_related_fields(self, fields, id_field):
return [k for (k, v) in fields
if k != id_field
and (isinstance(v, HyperlinkedRelatedField)
or isinstance(v, HyperlinkedIdentityField)
or isinstance(v, ItemLinkField)
or (isinstance(v, ManyRelatedField)
and isinstance(v.child_relation, HyperlinkedRelatedField)))]