diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md index 1d4c34cb9..6bc572fa4 100644 --- a/docs/api-guide/fields.md +++ b/docs/api-guide/fields.md @@ -314,6 +314,7 @@ By default, `HyperlinkedRelatedField` is read-write, although you can change thi **Arguments**: * `view_name` - The view name that should be used as the target of the relationship. **required**. +* `view_namespace` - The namespace of the view, used as the target of the relationship. The default namespace can be set as HyperlinkedModelSerializerOptions attribute. If not set, it's `None`. * `format` - If using format suffixes, hyperlinked fields will use the same format suffix for the target unless overridden by using the `format` argument. * `queryset` - By default `ModelSerializer` classes will use the default queryset for the relationship. `Serializer` classes must either set a queryset explicitly, or set `read_only=True`. * `slug_field` - The field on the target that should be used for the lookup. Default is `'slug'`. @@ -329,6 +330,7 @@ This field is always read-only. **Arguments**: * `view_name` - The view name that should be used as the target of the relationship. **required**. +* `view_namespace` - The namespace of the view, used as the target of the relationship. The default namespace can be set as HyperlinkedModelSerializerOptions attribute. If not set, it's `None`. * `format` - If using format suffixes, hyperlinked fields will use the same format suffix for the target unless overridden by using the `format` argument. * `slug_field` - The field on the target that should be used for the lookup. Default is `'slug'`. * `pk_url_kwarg` - The named url parameter for the pk field lookup. Default is `pk`. diff --git a/rest_framework/fields.py b/rest_framework/fields.py index a7652db0c..97b60a004 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -547,7 +547,7 @@ class HyperlinkedRelatedField(RelatedField): return self.slug_field def to_native(self, obj): - view_namespace = self.view_namespace + view_namespace = self.view_namespace or getattr(self.parent.opts, 'view_namespace', None) view_name = self.view_name if view_namespace: view_name = '%(namespace)s:%(name)' % {'namespace': view_namespace, 'name':view_name} @@ -659,7 +659,7 @@ class HyperlinkedIdentityField(Field): def field_to_native(self, obj, field_name): request = self.context.get('request', None) format = self.format or self.context.get('format', None) - view_namespace = self.view_namespace + view_namespace = self.view_namespace or getattr(self.parent.opts, 'view_namespace', None) view_name = self.view_name if view_namespace: view_name = '%(namespace)s:%(name)' % {'namespace': view_namespace, 'name':view_name}