SimpleMetadata: create schema endpoints doesn't work as expected for generic viewset with UpdateModelMixin #8597
Unanswered
timur-orudzhov
asked this question in
Question & Answer
Replies: 2 comments
-
I think problem is located in def determine_actions(self, request, view):
"""
For generic class based views we return information about
the fields that are accepted for 'PUT' and 'POST' methods.
"""
actions = {}
for method in {'PUT', 'POST'} & set(view.allowed_methods):
view.request = clone_request(request, method)
try:
# Test global permissions
if hasattr(view, 'check_permissions'):
view.check_permissions(view.request)
# Test object permissions
if method == 'PUT' and hasattr(view, 'get_object'):
view.get_object()
except (exceptions.APIException, PermissionDenied, Http404):
pass
else:
# If user has appropriate permissions for the view, include
# appropriate metadata about the fields that should be supplied.
serializer = view.get_serializer()
actions[method] = self.get_serializer_info(serializer)
finally:
view.request = request
return actions |
Beta Was this translation helpful? Give feedback.
0 replies
-
My workaround for this issue, maybe it would useful for others: class ApiMetadata(SimpleMetadata):
"""Metadata class that supports custom schema endpoints."""
@staticmethod
def _determine_view_methods(view):
"""Determine http methods that viewset is supported."""
methods = []
if hasattr(view, 'update'):
methods.append(HTTP_PUT_METHOD)
if hasattr(view, 'create'):
methods.append(HTTP_POST_METHOD)
return methods
def determine_actions(self, request, view):
actions = {}
for method in self._determine_view_methods(view):
view.request = clone_request(request, method)
try:
# Test global permissions
if hasattr(view, 'check_permissions'):
view.check_permissions(view.request)
except (exceptions.APIException, PermissionDenied, Http404):
pass
else:
# If user has appropriate permissions for the view, include
# appropriate metadata about the fields that should be supplied.
serializer = view.get_serializer()
actions[method] = self.get_serializer_info(serializer)
finally:
view.request = request
return actions and that way we can use it: class MetaDataViewSetMixin:
"""Metadata mixin."""
@action(
methods=[HTTP_GET_METHOD],
detail=False,
pagination_class=None,
permission_classes=[],
)
def meta(self, request):
meta = self.metadata_class()
data = meta.determine_metadata(request, self)
return Response(data) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Steps to reproduce
Define custom schema (aka api resource meta data) as described in the docs for generic viewset with
UpdateModelMixin
– https://www.django-rest-framework.org/api-guide/metadata/#creating-schema-endpointsCheck out the meta endpoint v1/api/resource_name/schema/ and that endpoint doesn't return any info about actions PUT/PATCH.
Is there way to define meta data endpoint about PUT/PATCH for the update/partial update generic viewset?
Beta Was this translation helpful? Give feedback.
All reactions