[Django] #33086: Postgres Contrib ArrayField raises error on serialization with "django.core.serializers.json"

0 views
Skip to first unread message

Django

unread,
Sep 6, 2021, 12:34:00 PM9/6/21
to django-...@googlegroups.com
#33086: Postgres Contrib ArrayField raises error on serialization with
"django.core.serializers.json"
-------------------------------------+-------------------------------------
Reporter: Anudeep | Owner: < anudeepsamaiya >
Samaiya |
Type: Bug | Status: assigned
Component: | Version: 3.2
contrib.postgres |
Severity: Normal | Keywords: postgres, json
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
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)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33086>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 6, 2021, 12:48:13 PM9/6/21
to django-...@googlegroups.com
#33086: Postgres Contrib ArrayField raises error on serialization with
"django.core.serializers.json"
-------------------------------------+-------------------------------------
Reporter: Anudeep Samaiya | Owner: Anudeep

| Samaiya
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 3.2
Severity: Normal | Resolution:

Keywords: postgres, json | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Anudeep Samaiya):

* cc: Anudeep Samaiya (added)
* owner: < anudeepsamaiya > => Anudeep Samaiya


--
Ticket URL: <https://code.djangoproject.com/ticket/33086#comment:1>

Django

unread,
Sep 7, 2021, 12:32:53 AM9/7/21
to django-...@googlegroups.com
#33086: ArrayField with UUIDField raises error on serialization with
"django.core.serializers.json"
-------------------------------------+-------------------------------------

Reporter: Anudeep Samaiya | Owner: Anudeep
| Samaiya
Type: Bug | Status: closed
Component: contrib.postgres | Version: 3.2
Severity: Normal | Resolution: needsinfo

Keywords: postgres, json | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* 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>

Django

unread,
Sep 7, 2021, 2:39:35 AM9/7/21
to django-...@googlegroups.com
#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:

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>

Django

unread,
Sep 7, 2021, 3:32:33 AM9/7/21
to django-...@googlegroups.com
#33086: ArrayField with JSONField and custom encoder raises error on serialization
with "django.core.serializers.json".

-------------------------------------+-------------------------------------
Reporter: Anudeep Samaiya | Owner: Anudeep
| Samaiya
Type: Bug | Status: closed
Component: contrib.postgres | Version: 3.2
Severity: Normal | Resolution: wontfix

Keywords: postgres, json | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* 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>

Reply all
Reply to author
Forward
0 new messages