2022-11-18 11:24:14 -05:00
|
|
|
from django.contrib.postgres.fields import ArrayField
|
2022-11-29 09:18:03 -05:00
|
|
|
from django.core.serializers.json import Deserializer, Serializer as Serializer_ # noqa
|
2022-11-18 11:24:14 -05:00
|
|
|
from django.utils.encoding import is_protected_type
|
|
|
|
|
2022-11-29 09:18:03 -05:00
|
|
|
# NOTE: Module must contain both Serializer and Deserializer
|
|
|
|
|
2022-11-18 11:24:14 -05:00
|
|
|
|
|
|
|
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)
|