Fixed #32179 -- Added JSONObject database function.

This commit is contained in:
Artur Beltsov 2020-11-08 12:52:34 +05:00 committed by Mariusz Felisiak
parent adb40d217e
commit 48b4bae983
8 changed files with 161 additions and 2 deletions

View file

@ -148,6 +148,29 @@ and ``comment.modified``.
The PostgreSQL behavior can be emulated using ``Coalesce`` if you know
a sensible minimum value to provide as a default.
``JSONObject``
--------------
.. class:: JSONObject(**fields)
.. versionadded:: 3.2
Takes a list of key-value pairs and returns a JSON object containing those
pairs.
Usage example::
>>> from django.db.models import F
>>> from django.db.models.functions import JSONObject, Lower
>>> Author.objects.create(name='Margaret Smith', alias='msmith', age=25)
>>> author = Author.objects.annotate(json_object=JSONObject(
... name=Lower('name'),
... alias='alias',
... age=F('age') * 2,
... )).get()
>>> author.json_object
{'name': 'margaret smith', 'alias': 'msmith', 'age': 50}
``Least``
---------

View file

@ -367,6 +367,8 @@ Models
block exits without errors. A nested atomic block marked as durable will
raise a ``RuntimeError``.
* Added the :class:`~django.db.models.functions.JSONObject` database function.
Pagination
~~~~~~~~~~