mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-08 06:14:47 +03:00
pass instance to view.get_serializer in metadata's determine_actions method since choices (especially querysets of related fields) can often depend on the instance
This commit is contained in:
parent
450c541462
commit
c5d6ecc84c
|
@ -74,19 +74,23 @@ class SimpleMetadata(BaseMetadata):
|
||||||
actions = {}
|
actions = {}
|
||||||
for method in set(['PUT', 'POST']) & set(view.allowed_methods):
|
for method in set(['PUT', 'POST']) & set(view.allowed_methods):
|
||||||
view.request = clone_request(request, method)
|
view.request = clone_request(request, method)
|
||||||
|
instance = None
|
||||||
try:
|
try:
|
||||||
# Test global permissions
|
# Test global permissions
|
||||||
if hasattr(view, 'check_permissions'):
|
if hasattr(view, 'check_permissions'):
|
||||||
view.check_permissions(view.request)
|
view.check_permissions(view.request)
|
||||||
# Test object permissions
|
# Test object permissions
|
||||||
if method == 'PUT' and hasattr(view, 'get_object'):
|
if method == 'PUT' and hasattr(view, 'get_object'):
|
||||||
view.get_object()
|
instance = view.get_object()
|
||||||
except (exceptions.APIException, PermissionDenied, Http404):
|
except (exceptions.APIException, PermissionDenied, Http404):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
# If user has appropriate permissions for the view, include
|
# If user has appropriate permissions for the view, include
|
||||||
# appropriate metadata about the fields that should be supplied.
|
# appropriate metadata about the fields that should be supplied.
|
||||||
serializer = view.get_serializer()
|
kwargs = {}
|
||||||
|
if instance is not None:
|
||||||
|
kwargs['instance'] = instance
|
||||||
|
serializer = view.get_serializer(**kwargs)
|
||||||
actions[method] = self.get_serializer_info(serializer)
|
actions[method] = self.get_serializer_info(serializer)
|
||||||
finally:
|
finally:
|
||||||
view.request = request
|
view.request = request
|
||||||
|
|
Loading…
Reference in New Issue
Block a user