#32179: Add JSONObject Func
-------------------------------------+-------------------------------------
               Reporter:             |          Owner:  nobody
  Chiorufarewerin                    |
                   Type:  New        |         Status:  new
  feature                            |
              Component:  Database   |        Version:  master
  layer (models, ORM)                |       Keywords:  json_object,
               Severity:  Normal     |  jsonb_build_object, JSONObject
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 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()
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32179>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/058.b5408975eb4c98ae635ba7f8ec258692%40djangoproject.com.

Reply via email to