I have a Django model in which I would like to save schema-less key-value metadata, so I'm using a django.contrib.postgres.fields.JSONField.
The key-value data is a Pandas series, which is not JSON-serializable by default (due to numpy.int64, numpy.float64 types), so I use the handy series.to_json() which returns an already serialized JSON string.
To save it to my model, I run json.loads on it. But I know that json.dumps is called on this when the data is saved to Postgres.
How can I avoid this unnecessary deserialization/re-serialization step?
Code example:
def create_model(pandas_series):
mdl = Model()
metadata = pandas_series.to_json() # gives a JSON string
mdl.metadata = json.loads(metadata) # string->dict, then dict->string
mdl.save()
metadata = ...field defintion)? Is thisdjango.contrib.postgres.fields.jsonb.JSONFieldor a third-party package?