فهرست منبع

Fixes #9223: Fix serialization of array field values in change log

jeremystretch 3 سال پیش
والد
کامیت
0885333b11
3فایلهای تغییر یافته به همراه24 افزوده شده و 0 حذف شده
  1. 1 0
      docs/release-notes/version-3.3.md
  2. 4 0
      netbox/netbox/settings.py
  3. 19 0
      netbox/utilities/serializers/json.py

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

@@ -9,6 +9,7 @@
 ### Bug Fixes
 
 * [#6389](https://github.com/netbox-community/netbox/issues/6389) - Call `snapshot()` on object when processing deletions
+* [#9223](https://github.com/netbox-community/netbox/issues/9223) - Fix serialization of array field values in change log
 * [#9878](https://github.com/netbox-community/netbox/issues/9878) - Fix spurious error message when rendering REST API docs
 * [#10236](https://github.com/netbox-community/netbox/issues/10236) - Fix TypeError exception when viewing PDU configured for three-phase power
 * [#10579](https://github.com/netbox-community/netbox/issues/10579) - Mark cable traces terminating to a provider network as complete

+ 4 - 0
netbox/netbox/settings.py

@@ -445,6 +445,10 @@ EXEMPT_PATHS = (
     f'/{BASE_PATH}metrics',
 )
 
+SERIALIZATION_MODULES = {
+    'json': 'utilities.serializers.json',
+}
+
 
 #
 # Sentry

+ 19 - 0
netbox/utilities/serializers/json.py

@@ -0,0 +1,19 @@
+from django.contrib.postgres.fields import ArrayField
+from django.core.serializers.json import Serializer as Serializer_
+from django.utils.encoding import is_protected_type
+
+
+class Serializer(Serializer_):
+    """
+    Custom extension of Django's JSON serializer to support ArrayFields (see
+    https://code.djangoproject.com/ticket/33974).
+    """
+    def _value_from_field(self, obj, field):
+        value = field.value_from_object(obj)
+
+        # Handle ArrayFields of protected types
+        if type(field) is ArrayField:
+            if not value or is_protected_type(value[0]):
+                return value
+
+        return value if is_protected_type(value) else field.value_to_string(obj)