So I currently use for build json object for postgres like:
Func(
Value('name'), 'authors__name',
Value('alias'), 'authors__alias',
function='jsonb_build_object'
)
I checked other dbs formats for this, and all of this have that functional:
https://www.sqlite.org/json1.html#jobj
https://dev.mysql.com/doc/refman/8.0/en/json-creation-functions.html#function_json-object
https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/JSON_OBJECT.html
I use it for related objects, for example:
class Article(models.Model):
title = models.CharField(max_length=50)
class Product(models.Model):
article = models.ForeignKey(Article, related_name='products', on_delete=models.CASCADE)
name = models.CharField(max_length=255)
Product.objects.annotate(
article_json=Func(
Value('title'), 'article__title',
function='JSONB_BUILD_OBJECT',
)
).values('name', 'article_json').first()
So, with JSONObject will be something like this:
Product.objects.annotate(
article_json=JSONObject(
title='article__title',
)
).values('name', 'article_json').first()
In 48b4bae9: