Added get_url hook to HyperlinkedIdentityField in line with HyperlinedRelatedField. Closes #883

This commit is contained in:
Tom Christie 2013-05-29 14:08:48 +01:00
parent 85fe7197bf
commit c98ac3e83e

View File

@ -537,6 +537,25 @@ class HyperlinkedIdentityField(Field):
if format and self.format and self.format != format: if format and self.format and self.format != format:
format = self.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) lookup_field = getattr(obj, self.lookup_field)
kwargs = {self.lookup_field: lookup_field} kwargs = {self.lookup_field: lookup_field}
try: try:
@ -562,7 +581,7 @@ class HyperlinkedIdentityField(Field):
except NoReverseMatch: except NoReverseMatch:
pass pass
raise Exception('Could not resolve URL for field using view name "%s"' % view_name) raise NoReverseMatch()
### Old-style many classes for backwards compat ### Old-style many classes for backwards compat