Add converter for django 3.1 JSONField

This commit is contained in:
Nikolai R Kristiansen 2020-08-02 14:10:07 +02:00
parent b552dcac24
commit 25de313645
No known key found for this signature in database
GPG Key ID: 1A24E7511E55EF87
3 changed files with 25 additions and 6 deletions

View File

@ -8,8 +8,14 @@ try:
from django.contrib.postgres.fields import (
ArrayField,
HStoreField,
JSONField,
JSONField as PGJSONField,
RangeField,
)
except ImportError:
ArrayField, HStoreField, JSONField, RangeField = (MissingType,) * 4
ArrayField, HStoreField, PGJSONField, RangeField = (MissingType,) * 4
try:
# JSONField is only available from Django 3.1
from django.contrib.fields import JSONField
except ImportError:
JSONField = MissingType

View File

@ -24,7 +24,7 @@ from graphene.utils.str_converters import to_camel_case
from graphql import assert_valid_name
from .settings import graphene_settings
from .compat import ArrayField, HStoreField, JSONField, RangeField
from .compat import ArrayField, HStoreField, JSONField, PGJSONField, RangeField
from .fields import DjangoListField, DjangoConnectionField
from .utils import import_single_dispatch
from .utils.str_converters import to_const
@ -267,8 +267,9 @@ def convert_postgres_array_to_list(field, registry=None):
@convert_django_field.register(HStoreField)
@convert_django_field.register(PGJSONField)
@convert_django_field.register(JSONField)
def convert_postgres_field_to_string(field, registry=None):
def convert_pg_and_json_field_to_string(field, registry=None):
return JSONString(description=field.help_text, required=not field.null)

View File

@ -11,7 +11,14 @@ from graphene.relay import ConnectionField, Node
from graphene.types.datetime import Date, DateTime, Time
from graphene.types.json import JSONString
from ..compat import ArrayField, HStoreField, JSONField, MissingType, RangeField
from ..compat import (
ArrayField,
HStoreField,
JSONField,
PGJSONField,
MissingType,
RangeField,
)
from ..converter import (
convert_django_field,
convert_django_field_with_choices,
@ -348,8 +355,13 @@ def test_should_postgres_hstore_convert_string():
assert_conversion(HStoreField, JSONString)
@pytest.mark.skipif(JSONField is MissingType, reason="JSONField should exist")
@pytest.mark.skipif(PGJSONField is MissingType, reason="PGJSONField should exist")
def test_should_postgres_json_convert_string():
assert_conversion(PGJSONField, JSONString)
@pytest.mark.skipif(JSONField is MissingType, reason="JSONField should exist")
def test_should_json_convert_string():
assert_conversion(JSONField, JSONString)