Opened 4 years ago

Closed 4 years ago

#32179 closed New feature (fixed)

Add JSONObject Func

Reported by: Chiorufarewerin Owned by: Chiorufarewerin
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: json_object, jsonb_build_object, JSONObject
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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()

Change History (6)

comment:1 by Chiorufarewerin, 4 years ago

Owner: changed from nobody to Chiorufarewerin
Status: newassigned

comment:2 by Simon Charette, 4 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Simon Charette, 4 years ago

Has patch: set

comment:4 by Mariusz Felisiak, 4 years ago

Patch needs improvement: set

comment:5 by Mariusz Felisiak, 4 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 48b4bae9:

Fixed #32179 -- Added JSONObject database function.

Note: See TracTickets for help on using tickets.
Back to Top