Jelajahi Sumber

Monkey-patch DRF to treat bulk_destroy as a built-in operation

Jeremy Stretch 5 tahun lalu
induk
melakukan
c1b57af771
2 mengubah file dengan 18 tambahan dan 0 penghapusan
  1. 11 0
      netbox/netbox/api.py
  2. 7 0
      netbox/netbox/settings.py

+ 11 - 0
netbox/netbox/api.py

@@ -4,11 +4,22 @@ from rest_framework import authentication, exceptions
 from rest_framework.pagination import LimitOffsetPagination
 from rest_framework.permissions import DjangoObjectPermissions, SAFE_METHODS
 from rest_framework.renderers import BrowsableAPIRenderer
+from rest_framework.schemas import coreapi
 from rest_framework.utils import formatting
 
 from users.models import Token
 
 
+def is_custom_action(action):
+    return action not in {
+        'retrieve', 'list', 'create', 'update', 'partial_update', 'destroy', 'bulk_destroy'
+    }
+
+
+# Monkey-patch DRF to treat bulk_destroy() as a non-custom action (see #3436)
+coreapi.is_custom_action = is_custom_action
+
+
 #
 # Renderers
 #

+ 7 - 0
netbox/netbox/settings.py

@@ -472,6 +472,13 @@ REST_FRAMEWORK = {
     'DEFAULT_VERSION': REST_FRAMEWORK_VERSION,
     'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.AcceptHeaderVersioning',
     'PAGE_SIZE': PAGINATE_COUNT,
+    'SCHEMA_COERCE_METHOD_NAMES': {
+        # Default mappings
+        'retrieve': 'read',
+        'destroy': 'delete',
+        # Custom operations
+        'bulk_destroy': 'bulk_delete',
+    },
     'VIEW_NAME_FUNCTION': 'netbox.api.get_view_name',
 }