[Django] #29096: JSONField() as base_field for ArrayField() for creating an object array

9 views
Skip to first unread message

Django

unread,
Jan 31, 2018, 2:17:17 PM1/31/18
to django-...@googlegroups.com
#29096: JSONField() as base_field for ArrayField() for creating an object array
-------------------------------------+-------------------------------------
Reporter: Jim Biggs | Owner: nobody
Type: | Status: new
Uncategorized |
Component: Core | Version: 2.0
(Serialization) | Keywords: serialization,
Severity: Normal | JSONField, ArrayField
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Current Environment:
• Latest Django-2
• Latest Django Rest Framework
• PostgreSQL-9.5
• Ubuntu-16.04

My understanding of the JSONField() is that it is a field container for a
single JSON object. My use-case requires an array of objects. So, I
created an ArrayField() that nests a JSONField() as its model base_field
-- as shown in the code block, below:

{{{
questions = ArrayField(JSONField(null=True, blank=True, default=dict),
max_length=200, blank=True, default=[])
}}}

Everything seems to work fine with the exception of the JSON object being
stringified, as shown below:


{{{
"questions": [
"{
\"question\": \"What is the answer to everything?\",
\"answer\": \"42\"
}",
"{
\"question\": \"What year is it?\",
\"answer\": \"2018\"
}"
],
}}}


I believe the inserted backslashes (\) are a result of the serialization
process. My question is whether there is a better way for me to define
this object array model field?

Regards... Jim

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

Django

unread,
Jan 31, 2018, 2:21:00 PM1/31/18
to django-...@googlegroups.com
#29096: JSONField() as base_field for ArrayField() for creating an object array
-------------------------------------+-------------------------------------
Reporter: Jim Biggs | Owner: nobody
Type: Bug | Status: closed
Component: contrib.postgres | Version: 2.0
Severity: Normal | Resolution: duplicate
Keywords: serialization, | Triage Stage:
JSONField, ArrayField | Unreviewed
Has patch: 0 | Needs documentation: 0

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

* status: new => closed
* type: Uncategorized => Bug
* component: Core (Serialization) => contrib.postgres
* resolution: => duplicate


Comment:

Looks like a duplicate of #28291. (For usage questions, please use
[wiki:TicketClosingReasons/UseSupportChannels our support channels].)

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

Django

unread,
Jan 31, 2018, 2:28:40 PM1/31/18
to django-...@googlegroups.com
#29096: JSONField() as base_field for ArrayField() for creating an object array
-------------------------------------+-------------------------------------
Reporter: Jim Biggs | Owner: nobody
Type: Bug | Status: closed
Component: contrib.postgres | Version: 2.0
Severity: Normal | Resolution: duplicate
Keywords: serialization, | Triage Stage:
JSONField, ArrayField | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Jim Biggs:

Old description:

New description:

Current Environment:
• Latest Django-2
• Latest Django Rest Framework
• PostgreSQL-9.5
• Ubuntu-16.04

My understanding of the JSONField() is that it is a field container for a
single JSON object. My use-case requires an array of objects. So, I
created an ArrayField() that nests a JSONField() as its model base_field
-- as shown in the code block, below:

{{{
questions = ArrayField(JSONField(null=True, blank=True, default=dict),
max_length=200, blank=True, default=[])
}}}

Everything seems to work fine with the exception of the JSON object being
stringified, as shown below:


{{{
"questions": [
"{
\"question\": \"What is the answer to everything?\",
\"answer\": \"42\"
}",
"{
\"question\": \"What year is it?\",
\"answer\": \"2018\"
}"
]
}}}

I believe the inserted backslashes (\) are a result of the serialization
process. My question is whether there is a better way for me to define
this object array model field?

Regards... Jim

--

--
Ticket URL: <https://code.djangoproject.com/ticket/29096#comment:2>

Reply all
Reply to author
Forward
0 new messages