Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#29096 closed Bug (duplicate)

JSONField() as base_field for ArrayField() for creating an object array

Reported by: Jim Biggs Owned by: nobody
Component: contrib.postgres Version: 2.0
Severity: Normal Keywords: serialization, JSONField, ArrayField
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Jim Biggs)

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

Change History (2)

comment:1 by Tim Graham, 6 years ago

Component: Core (Serialization)contrib.postgres
Resolution: duplicate
Status: newclosed
Type: UncategorizedBug

Looks like a duplicate of #28291. (For usage questions, please use our support channels.)

comment:2 by Jim Biggs, 6 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top