Raise warnings if 'request' not in context for hyperlinked fields.

This commit is contained in:
Tom Christie 2013-02-12 13:54:50 +00:00
parent 388e617366
commit 41ac1e8f32

View File

@ -311,6 +311,13 @@ class HyperlinkedRelatedField(RelatedField):
view_name = self.view_name
request = self.context.get('request', None)
format = self.format or self.context.get('format', None)
if request is None:
warnings.warn("Using `HyperlinkedRelatedField` without including the "
"request in the serializer context is due to be deprecated. "
"Add `context={'request': request}` when instantiating the serializer.",
PendingDeprecationWarning, stacklevel=4)
pk = getattr(obj, 'pk', None)
if pk is None:
return
@ -420,6 +427,12 @@ class HyperlinkedIdentityField(Field):
view_name = self.view_name or self.parent.opts.view_name
kwargs = {self.pk_url_kwarg: obj.pk}
if request is None:
warnings.warn("Using `HyperlinkedIdentityField` without including the "
"request in the serializer context is due to be deprecated. "
"Add `context={'request': request}` when instantiating the serializer.",
PendingDeprecationWarning, stacklevel=4)
# By default use whatever format is given for the current context
# unless the target is a different type to the source.
#