aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Christie2013-01-03 21:46:18 +0000
committerTom Christie2013-01-03 21:49:29 +0000
commitbfea7f64ee66cf96c6802ab68d03983aa5bf997f (patch)
tree7079684cc2cef2beb1391db849dea0a505ddb955
parent92ae08207a7d588aef05eecf6826765ac6caf299 (diff)
downloaddjango-rest-framework-bfea7f64ee66cf96c6802ab68d03983aa5bf997f.tar.bz2
Tweak behavior of hyperlinked fields that include an explicit format suffix.
-rw-r--r--docs/topics/release-notes.md2
-rw-r--r--rest_framework/relations.py16
2 files changed, 16 insertions, 2 deletions
diff --git a/docs/topics/release-notes.md b/docs/topics/release-notes.md
index b0100179..b5d5ae02 100644
--- a/docs/topics/release-notes.md
+++ b/docs/topics/release-notes.md
@@ -66,7 +66,7 @@ This change will not affect user code, so long as it's following the recommended
* Bugfix: Ensure read-only fields don't have model validation applied.
* Bugfix: Fix hyperlinked fields in paginated results.
-## 2.1.9
+### 2.1.9
**Date**: 11th Dec 2012
diff --git a/rest_framework/relations.py b/rest_framework/relations.py
index 9b3a7790..686dcf04 100644
--- a/rest_framework/relations.py
+++ b/rest_framework/relations.py
@@ -407,6 +407,7 @@ class HyperlinkedIdentityField(Field):
# TODO: Make view_name mandatory, and have the
# HyperlinkedModelSerializer set it on-the-fly
self.view_name = kwargs.pop('view_name', None)
+ # Optionally the format of the target hyperlink may be specified
self.format = kwargs.pop('format', None)
self.slug_field = kwargs.pop('slug_field', self.slug_field)
@@ -418,9 +419,22 @@ 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)
+ format = self.context.get('format', None)
view_name = self.view_name or self.parent.opts.view_name
kwargs = {self.pk_url_kwarg: obj.pk}
+
+ # By default use whatever format is given for the current context
+ # unless the target is a different type to the source.
+ #
+ # Eg. Consider a HyperlinkedIdentityField pointing from a json
+ # representation to an html property of that representation...
+ #
+ # '/snippets/1/' should link to '/snippets/1/highlight/'
+ # ...but...
+ # '/snippets/1/.json' should link to '/snippets/1/highlight/.html'
+ if format and self.format and self.format != format:
+ format = self.format
+
try:
return reverse(view_name, kwargs=kwargs, request=request, format=format)
except: