Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#28388 closed New feature (wontfix)

JSONField cannot be overridden to use OrderedDict as python-representation of objects of JSON

Reported by: Vladimir Chub Owned by:
Component: contrib.postgres Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I tried to override django.contrib.postgres.fields.JSONField to use OrderedDict instead of dict when field value is representing as Python-object. According to the source code, it was necessary override django.contrib.postgres.fields.jsonb.JSONField and its formfield form django.contrib.postgres.forms.jsonb.JSONField. I changed json.loads(value) to json.loads(value, object_pairs_hook=OrderedDict) in multiple places but it has no effect. Having dealt with the source code, I came to the conclusion that there is no possibility (or too complexity, so I don't know how) to override this behavior of JSONField.
Please, implement the feature which will allow override JSONField's python-representation

Change History (4)

comment:1 by Dark Goat, 7 years ago

I think there is a common way to resolve this, the code down below should work.

your_filed = JSONField(default=collections.OrderedDict)

comment:2 by Tim Graham, 7 years ago

Resolution: wontfix
Status: newclosed
Summary: JSONFIeld cannot be overridden to use OrderedDict as python-representation of objects of JSONJSONField cannot be overridden to use OrderedDict as python-representation of objects of JSON

Django's JSONField uses the jsonb data type. According to PostgreSQL's documentation, jsonb "does not preserve the order of object keys" so I don't think this is possible. Perhaps there are other custom fields that use the PostgreSQL's json datatype (which does preserve ordering) that will work for your use case.

comment:3 by Vladimir Chub, 7 years ago

Perhaps there are other custom fields that use the PostgreSQL's json datatype (which does preserve ordering) that will work for your use case.

Of course there is a json field in PostgreSQl supporting ordering https://www.postgresql.org/docs/9.6/static/datatype-json.html

Because the json type stores an exact copy of the input text, it will preserve semantically-insignificant white space between tokens, as well as the order of keys within JSON objects

Django is now using jsonrb as JSONField, but also something like OrderedJSONField mapped with PostgreSQL json field can be implemented. Are there any chances for if?

Last edited 7 years ago by Vladimir Chub (previous) (diff)

comment:4 by Tim Graham, 7 years ago

I'm not sure. You could try to implement it and then write to the DevelopersMailingList with the proposal.

Note: See TracTickets for help on using tickets.
Back to Top