﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
32135	JSONField db_type `jsonb`==>`json` for sequential data no longer works	Michael Anuzis	nobody	"Issue: django.contrib.postgres.fields.JSONField in Django3.0.6 allows overriding db_type `jsonb` to `json`, which allows preserving the order of items in  nested OrderedDicts (ref: https://stackoverflow.com/a/57941588/1377763 ). Django3.1.2 appears to force use of `jsonb` by default (ref: https://docs.djangoproject.com/en/3.1/ref/contrib/postgres/fields/#jsonfield,  ""JSONField uses jsonb""), losing the order of items in the JSON, apparently providing no alternative option to retain it, and not supporting previous workarounds.

Steps to reproduce:
1) Create a model using a JSONField and attempt to override `jsonb` to `json` using the RawJSONField example in the stackoverflow link above.
2) Initialize an instance of the model with data in the JSONField involving OrderedDicts (example below you can copy/paste)
3) Attempt to retrieve the model, read, and/or update the field. For example, via `MyJSONModel.objects.get(user=user)`

Error:
- TypeError: the JSON object must be str, bytes or bytearray, not dict
- Exception location: /usr/lib/python3.8/json/__init__.py, line 341, in loads


Since the RawJSONField resulting from the above workaround appears to require a string (akin to storing json in a TextField...), I attempted to resolve this by manually converting the field's data via json.loads() and json.dumps() at each interaction with the field, but this resulted in continued errors of the same type and did not appear to have a clear fix. I've abandoned 3.0==>3.1 upgrade for now as the ability to retain the sequence of data in a JSONField is critical, 3.1 documentation doesn't describe how to force `jsonb` ==> `json`, and suggested workarounds via stackoverflow do not transition smoothly between versions.


Example JSON data structure for reproducing bug:
from collections import OrderedDict
progress = OrderedDict([
('BODY1_SLUG', {'name': 'BODY1_NAME', 'n_videos': 19, 'kbranches': OrderedDict([
('BRANCH1_SLUG', {'name': 'BRANCH1_NAME', 'n_videos': 7, 'vids_duration_mins': 20, 'completed': 0, 'concepts': OrderedDict([
('CONCEPT1_SLUG', {'name': 'CONCEPT1_NAME', 'videos': [{'name': 'A', 'url': 'UA', 'duration_in_secs': 120, 'viewed': 1}, {...}]}),
('CONCEPT2_SLUG', {'name': 'CONCEPT2_NAME', 'videos': [{'name': 'B', 'url': 'UB', 'duration_in_secs': 120, 'viewed': 0}, {...}]}),
])})
])})
])  # Initially provided with indentation, but the bug report parsing converted to block quotes."	Uncategorized	closed	Database layer (models, ORM)	3.1	Normal	invalid			Unreviewed	0	0	0	0	0	0
