
Django JSONStore: typed model fields backed by nested JSON
I have just released a new version of Django JSONStore.
Many Django projects keep one-off or fast-changing business data in a JSONField. This is flexible, but requires additional code when you need a ModelForm or want to edit the data in Django admin. Moreover JSON internal structure became exposed all over codebase.
JSONStore maps any path in the document to a typed virtual model field. These fields work as a regular data assessors, in ModelForms and Django admin like normal model fields. They also support filters, ordering, values() and values_list().
from django.db import models
import jsonstore
class Employee(models.Model):
data = models.JSONField(default=dict)
full_name = jsonstore.CharField(
max_length=250,
json_field_name="data",
json_key=("profile", "full_name"),
)
hire_date = jsonstore.DateField(
null=True,
json_field_name="data",
json_key=("profile", "hire_date"),
)
employee = Employee(full_name="Ann Lee")
employee.data
# {"profile": {"full_name": "Ann Lee"}}
Employee.objects.filter(full_name="Ann Lee").order_by("hire_date")
This keeps rest of code, free from knowledge of json internals, leaves your option to migrate to standalone Django model column later.
You can also expose a whole nested document as a typed jsonstore.EmbeddedModel with EmbeddedField, or a list of documents with EmbeddedListField. You even can use emulation of document-backed ForeignKey, OneToOneField and ManyToManyField fields.
Use JSONStore for business data that changes often and doesn't make sense to get aggregated data.
GitHub: https://github.com/viewflow/jsonstore
Website and examples: https://django-jsonstore.viewflow.io/