#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 , 7 years ago
comment:2 by , 7 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Summary: | JSONFIeld cannot be overridden to use OrderedDict as python-representation of objects of JSON → JSONField 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 , 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 of if?
comment:4 by , 7 years ago
I'm not sure. You could try to implement it and then write to the DevelopersMailingList with the proposal.
I think there is a common way to resolve this, the code down below should work.