Просмотр исходного кода

Fixes #5309: Avoid extraneous database queries when manipulating objects

Jeremy Stretch 5 лет назад
Родитель
Сommit
181f5c991b
2 измененных файлов с 8 добавлено и 6 удалено
  1. 1 0
      docs/release-notes/version-2.9.md
  2. 7 6
      netbox/extras/webhooks.py

+ 1 - 0
docs/release-notes/version-2.9.md

@@ -9,6 +9,7 @@
 ### Bug Fixes
 
 * [#5235](https://github.com/netbox-community/netbox/issues/5235) - Fix exception when editing IP address with a NAT IP assigned to a non-racked device
+* [#5309](https://github.com/netbox-community/netbox/issues/5309) - Avoid extraneous database queries when manipulating objects
 * [#5345](https://github.com/netbox-community/netbox/issues/5345) - Fix non-deterministic ordering of prefixes and IP addresses
 * [#5350](https://github.com/netbox-community/netbox/issues/5350) - Filter available racks by selected group when creating a rack reservation
 * [#5355](https://github.com/netbox-community/netbox/issues/5355) - Limit rack groups by selected site when editing a rack

+ 7 - 6
netbox/extras/webhooks.py

@@ -5,10 +5,10 @@ from django.contrib.contenttypes.models import ContentType
 from django.utils import timezone
 from django_rq import get_queue
 
-from extras.models import Webhook
 from utilities.api import get_serializer_for_model
 from .choices import *
-from .utils import FeatureQuery
+from .models import Webhook
+from .registry import registry
 
 
 def generate_signature(request_body, secret):
@@ -28,13 +28,14 @@ def enqueue_webhooks(instance, user, request_id, action):
     Find Webhook(s) assigned to this instance + action and enqueue them
     to be processed
     """
-    obj_type = ContentType.objects.get_for_model(instance.__class__)
-
-    webhook_models = ContentType.objects.filter(FeatureQuery('webhooks').get_query())
-    if obj_type not in webhook_models:
+    # Determine whether this type of object supports webhooks
+    app_label = instance._meta.app_label
+    model_name = instance._meta.model_name
+    if model_name not in registry['model_features']['webhooks'].get(app_label, []):
         return
 
     # Retrieve any applicable Webhooks
+    obj_type = ContentType.objects.get_for_model(instance)
     action_flag = {
         ObjectChangeActionChoices.ACTION_CREATE: 'type_create',
         ObjectChangeActionChoices.ACTION_UPDATE: 'type_update',