mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 01:57:00 +03:00
lookup_field on hyperlink serializers
This commit is contained in:
parent
2d44dc3f54
commit
538d2e35e7
|
@ -360,7 +360,15 @@ class HyperlinkedRelatedField(RelatedField):
|
|||
# Only try slug if it corresponds to an attribute on the object.
|
||||
kwargs = {self.slug_url_kwarg: slug}
|
||||
try:
|
||||
return reverse(view_name, kwargs=kwargs, request=request, format=format)
|
||||
ret = reverse(view_name, kwargs=kwargs, request=request, format=format)
|
||||
if self.slug_field == 'slug' and self.slug_url_kwarg == 'slug':
|
||||
# If the lookup succeeds using the default slug params,
|
||||
# then `slug_field` is being used implicitly, and we
|
||||
# we need to warn about the pending deprecation.
|
||||
msg = 'Implicit slug field hyperlinked fields are pending deprecation.' \
|
||||
'You should set `lookup_field=slug` on the HyperlinkedRelatedField.'
|
||||
warnings.warn(msg, PendingDeprecationWarning, stacklevel=2)
|
||||
return ret
|
||||
except NoReverseMatch:
|
||||
pass
|
||||
|
||||
|
|
|
@ -827,6 +827,7 @@ class HyperlinkedModelSerializerOptions(ModelSerializerOptions):
|
|||
def __init__(self, meta):
|
||||
super(HyperlinkedModelSerializerOptions, self).__init__(meta)
|
||||
self.view_name = getattr(meta, 'view_name', None)
|
||||
self.lookup_field = getattr(meta, 'slug_field', None)
|
||||
|
||||
|
||||
class HyperlinkedModelSerializer(ModelSerializer):
|
||||
|
@ -875,6 +876,9 @@ class HyperlinkedModelSerializer(ModelSerializer):
|
|||
if model_field:
|
||||
kwargs['required'] = not(model_field.null or model_field.blank)
|
||||
|
||||
if self.opts.lookup_field:
|
||||
kwargs['lookup_field'] = self.opts.lookup_field
|
||||
|
||||
return self._hyperlink_field_class(**kwargs)
|
||||
|
||||
def get_identity(self, data):
|
||||
|
|
Loading…
Reference in New Issue
Block a user