MCPcopy
hub / github.com/encode/django-rest-framework / get_url

Method get_url

rest_framework/relations.py:321–334  ·  view source on GitHub ↗

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.

(self, obj, view_name, request, format)

Source from the content-addressed store, hash-verified

319 raise exc.with_traceback(sys.exc_info()[2])
320
321 def get_url(self, obj, view_name, request, format):
322 """
323 Given an object, return the URL that hyperlinks to the object.
324
325 May raise a `NoReverseMatch` if the `view_name` and `lookup_field`
326 attributes are not configured to correctly match the URL conf.
327 """
328 # Unsaved objects will not yet have a valid URL.
329 if hasattr(obj, 'pk') and obj.pk in (None, ''):
330 return None
331
332 lookup_value = getattr(obj, self.lookup_field)
333 kwargs = {self.lookup_url_kwarg: lookup_value}
334 return self.reverse(view_name, kwargs=kwargs, request=request, format=format)
335
336 def to_internal_value(self, data):
337 request = self.context.get('request')

Callers 1

to_representationMethod · 0.95

Calls 1

reverseMethod · 0.45

Tested by

no test coverage detected