#33086: ArrayField with UUIDField raises error on serialization with
"django.core.serializers.json"
-------------------------------------+-------------------------------------
Reporter: Anudeep Samaiya | Owner: Anudeep
| Samaiya
Type: Bug | Status: new
Component: contrib.postgres | Version: 3.2
Severity: Normal | Resolution:
Keywords: postgres, json | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Anudeep Samaiya):
* status: closed => new
* needs_better_patch: 0 => 1
* needs_tests: 0 => 1
* easy: 0 => 1
* needs_docs: 0 => 1
* has_patch: 0 => 1
* resolution: needsinfo =>
Old description:
> The encoder is not supported currently, which causes the serialization of
> uuid fields missing.
>
> '''Current Implementation:'''
> {{{#!python
> def value_to_string(self, obj):
> values = []
> vals = self.value_from_object(obj)
> base_field = self.base_field
>
> for val in vals:
> if val is None:
> values.append(None)
> else:
> obj = AttributeSetter(base_field.attname, val)
> values.append(base_field.value_to_string(obj))
> return json.dumps(values)
> }}}
>
> '''Suggested Implementation:'''
> {{{#!python
> def value_to_string(self, obj, encoder=DjangoJSONEncoder):
> values = []
> vals = self.value_from_object(obj)
> base_field = self.base_field
>
> for val in vals:
> if val is None:
> values.append(None)
> else:
> obj = AttributeSetter(base_field.attname, val)
> values.append(base_field.value_to_string(obj))
> return json.dumps(values, cls=encoder)
> }}}
New description:
The encoder is not supported currently, which causes the serialization of
uuid fields missing.
'''Current Implementation:'''
{{{#!python
def value_to_string(self, obj):
values = []
vals = self.value_from_object(obj)
base_field = self.base_field
for val in vals:
if val is None:
values.append(None)
else:
obj = AttributeSetter(base_field.attname, val)
values.append(base_field.value_to_string(obj))
return json.dumps(values)
}}}
'''Suggested Implementation:'''
{{{#!python
def value_to_string(self, obj, encoder=DjangoJSONEncoder):
values = []
vals = self.value_from_object(obj)
base_field = self.base_field
for val in vals:
if val is None:
values.append(None)
else:
obj = AttributeSetter(base_field.attname, val)
values.append(base_field.value_to_string(obj))
return json.dumps(values, cls=encoder)
}}}
**How to reproduce:**
{{{#!python
import uuid
from django.contrib.postgres.fields import ArrayField, JSONField
from django.core.serializers.json import DjangoJSONEncoder
class Test(models.Model):
uuid = models.UUIDField(unique=True, default=uuid.uuid4,
editable=False)
data = ArrayField(
JSONField(encoder=DjangoJSONEncoder),
max_length=200,
blank=True,
default=list,
)
}}}
**Now to test do the following:**
{{{#!python
from decimal import Decimal
from django.core import serializers
from test.models import Test
test = Test(data=[{'threshold_amount_usd': Decimal('0'),
'taint_percent_threshold': Decimal('0')}])
serializers.serialize("json", (test,))
test = Test(data=[{'id': uuid4()}])
serializers.serialize("json", (test,))
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/33086#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/072.2691df8df717c42dd975b5dd8f3d5f11%40djangoproject.com.