aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/relations.py
diff options
context:
space:
mode:
authorTom Christie2013-05-29 14:08:48 +0100
committerTom Christie2013-05-29 14:09:31 +0100
commitc98ac3e83e537577cb90b2840c022e13e1c3c2b1 (patch)
tree461f66de278cab0caa6a8e316f6404b9fa9ea0be /rest_framework/relations.py
parent85fe7197bfecd80774f15db26833a44ba5f5d570 (diff)
downloaddjango-rest-framework-c98ac3e83e537577cb90b2840c022e13e1c3c2b1.tar.bz2
Added get_url hook to HyperlinkedIdentityField in line with HyperlinedRelatedField. Closes #883
Diffstat (limited to 'rest_framework/relations.py')
-rw-r--r--rest_framework/relations.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/rest_framework/relations.py b/rest_framework/relations.py
index 41707efc..42abf3ca 100644
--- a/rest_framework/relations.py
+++ b/rest_framework/relations.py
@@ -537,6 +537,25 @@ class HyperlinkedIdentityField(Field):
if format and self.format and self.format != format:
format = self.format
+ # Return the hyperlink, or error if incorrectly configured.
+ try:
+ return self.get_url(obj, view_name, request, format)
+ except NoReverseMatch:
+ msg = (
+ 'Could not resolve URL for hyperlinked relationship using '
+ 'view name "%s". You may have failed to include the related '
+ 'model in your API, or incorrectly configured the '
+ '`lookup_field` attribute on this field.'
+ )
+ raise Exception(msg % view_name)
+
+ def get_url(self, obj, view_name, request, format):
+ """
+ Given an object, return the URL that hyperlinks to the object.
+
+ May raise a `NoReverseMatch` if the `view_name` and `lookup_field`
+ attributes are not configured to correctly match the URL conf.
+ """
lookup_field = getattr(obj, self.lookup_field)
kwargs = {self.lookup_field: lookup_field}
try:
@@ -562,7 +581,7 @@ class HyperlinkedIdentityField(Field):
except NoReverseMatch:
pass
- raise Exception('Could not resolve URL for field using view name "%s"' % view_name)
+ raise NoReverseMatch()
### Old-style many classes for backwards compat