Django 1.8 provides HStoreField and Django 1.9 will provide JSONField (which uses jsonb) for PostgreSQL.
My understanding is that hstore is faster than json, but does not allow nesting and only allows strings.
When should one be used over the other? Should one be preferred over the other? Is hstore still the clear winner in performance compared to jsonb?
Typically I use a JSONField in two cases: To save a response from 3rd party APIs (e.g. as an audit trail) To save references to archived objects (e.g. when the live products in my db change but I still have orders referencing the product).
One of the cool features that was introduced in Django 3.1 was the JSONField . JSONField allows for you to store semi-structured data alongside other data fields in PostgreSQL, MySQL, and SQLite databases and supports introspection, lookups, and transforms.
If you need indexing, use jsonb
if you're on 9.4 or newer, otherwise hstore
. There's really no reason to prefer hstore
over jsonb
if both are available.
If you don't need indexing and fast processing and you're just storing and retrieving validated data, use plain json
. Unlike the other two options this preserves duplicate keys, formatting, key ordering, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With