'''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)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33086>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* cc: Anudeep Samaiya (added)
* owner: < anudeepsamaiya > => Anudeep Samaiya
--
Ticket URL: <https://code.djangoproject.com/ticket/33086#comment:1>
* status: assigned => closed
* has_patch: 1 => 0
* resolution: => needsinfo
* easy: 1 => 0
Comment:
Thanks for this report, however serialization works for me with:
- `uuid = models.UUIDField(default=uuid.uuid4)`
- `array = ArrayField(models.UUIDField(), default=list)`
- `nested_array = ArrayField(ArrayField(models.UUIDField()), null=True)`
I don't think you've explained the issue in enough detail to confirm a bug
in Django. Please reopen the ticket if you can debug your issue and
provide details about why and where Django is at fault. If you're having
trouble understanding how Django works, see
TicketClosingReasons/UseSupportChannels for ways to get help.
--
Ticket URL: <https://code.djangoproject.com/ticket/33086#comment:2>
* 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:
New description:
**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>
* status: new => closed
* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
* easy: 1 => 0
* needs_docs: 1 => 0
* has_patch: 1 => 0
* resolution: => wontfix
Comment:
Thanks for extra details. Unfortunately proposed change is backward
incompatible, we also cannot fix this by changing `to_python()` and
`value_to_string()` for `JSONField` (see discussions in
[https://github.com/django/django/pull/9801 PR9801],
[https://github.com/django/django/pull/9809 PR9809], and
[https://github.com/django/django/pull/11538 PR11538]). `JSONField` can
store JSON arrays so you should be able to use
`JSONField(encoder=DjangoJSONEncoder)` instead of
`ArrayField(JSONField(encoder=DjangoJSONEncoder))`. It also works with the
db round-trip
{{{
>>> test = Test.objects.create(data=[{'id': uuid.uuid4()}])
>>> test.refresh_from_db()
>>> serializers.serialize('json', (test,))
'[{"model": "test_33086.test", "pk": 16, "fields": {"array": "[]",
"nested_array": null, "data": "[{\\"id\\":
\\"2a7cc8f4-a541-49ce-8310-c73c6cc2dc4c\\"}]"}}]'
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33086#comment:4>