Fixed #12990, Refs #27694 -- Added JSONField model field.

Thanks to Adam Johnson, Carlton Gibson, Mariusz Felisiak, and Raphael
Michel for mentoring this Google Summer of Code 2019 project and
everyone else who helped with the patch.

Special thanks to Mads Jensen, Nick Pope, and Simon Charette for
extensive reviews.

Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
This commit is contained in:
sage 2019-06-09 07:56:37 +07:00 committed by Mariusz Felisiak
parent f97f71f592
commit 6789ded0a6
54 changed files with 2240 additions and 981 deletions

View file

@ -747,3 +747,10 @@ class ReturningModel(models.Model):
class NonIntegerPKReturningModel(models.Model):
created = CreatedField(editable=False, primary_key=True)
class JSONFieldNullable(models.Model):
json_field = models.JSONField(blank=True, null=True)
class Meta:
required_db_features = {'supports_json_field'}

View file

@ -3,11 +3,11 @@ import datetime
from django.core.exceptions import FieldDoesNotExist
from django.db.models import F
from django.db.models.functions import Lower
from django.test import TestCase
from django.test import TestCase, skipUnlessDBFeature
from .models import (
Article, CustomDbColumn, CustomPk, Detail, Individual, Member, Note,
Number, Order, Paragraph, SpecialCategory, Tag, Valid,
Article, CustomDbColumn, CustomPk, Detail, Individual, JSONFieldNullable,
Member, Note, Number, Order, Paragraph, SpecialCategory, Tag, Valid,
)
@ -228,3 +228,14 @@ class BulkUpdateTests(TestCase):
article.created = point_in_time
Article.objects.bulk_update(articles, ['created'])
self.assertCountEqual(Article.objects.filter(created=point_in_time), articles)
@skipUnlessDBFeature('supports_json_field')
def test_json_field(self):
JSONFieldNullable.objects.bulk_create([
JSONFieldNullable(json_field={'a': i}) for i in range(10)
])
objs = JSONFieldNullable.objects.all()
for obj in objs:
obj.json_field = {'c': obj.json_field['a'] + 1}
JSONFieldNullable.objects.bulk_update(objs, ['json_field'])
self.assertCountEqual(JSONFieldNullable.objects.filter(json_field__has_key='c'), objs)