The viewflow.jsonstore module provides a set of virtual Django Model fields that store their data inside a single JSON database column. This is suitable for storing simple business data, quick prototypes without requiring DB migrations, and replacing multi-table inheritance joins.
from viewflow import jsonstore
from django import forms
from django.contrib import admin
from django.db import models
class Employee(models.Model):
data = JSONField(default=dict)
full_name = jsonstore.CharField(max_length=250)
hire_date = jsonstore.DateField()
salary = jsonstore.DecimalField(max_digits=10, decimal_places=2)
The resulting model functions like a typical Django model. All virtual fields are available for constructing ModelForms, Viewsets, and Admin interfaces.
class EmployeeForm(forms.ModelForm):
class Meta:
model = Employee
fields = ['full_name', 'hire_date', 'salary']
@admin.register(Employee)
class EmployeeAdmin(admin.ModelAdmin):
list_display = ['full_name', 'hire_date']
fields = ['full_name', ('hire_date', 'salary')]
JSON Store plays well with proxy models. Additional virtual fields allows to model different real-life objects, without involving multi-table inheritance.
from django.contrib.auth.models import AbstractUser
from viewflow import jsonstore
class User(PolymorphicModel, AbstractUser):
data = jsonstore.JSONField(null=True, default=dict)
class Client(User):
address = jsonstore.CharField(max_length=250)
zip_code = jsonstore.CharField(max_length=250)
city = jsonstore.CharField(max_length=250)
vip = jsonstore.BooleanField()
class Meta:
proxy = True