{{{
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()
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32179>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* owner: nobody => Chiorufarewerin
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/32179#comment:1>
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/32179#comment:2>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32179#comment:3>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32179#comment:4>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/32179#comment:5>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"48b4bae983a1f9a73624fe62ef5bd1130b0dba39" 48b4bae9]:
{{{
#!CommitTicketReference repository=""
revision="48b4bae983a1f9a73624fe62ef5bd1130b0dba39"
Fixed #32179 -- Added JSONObject database function.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32179#comment:6>