Skip to content

Commit 67a0492

Browse files
authored
Add converter for django 3.1 JSONField (#1017)
1 parent 11dbde3 commit 67a0492

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
strategy:
99
max-parallel: 4
1010
matrix:
11-
django: ["1.11", "2.2", "3.0"]
11+
django: ["1.11", "2.2", "3.0", "3.1"]
1212
python-version: ["3.6", "3.7", "3.8"]
1313
include:
1414
- django: "1.11"

graphene_django/compat.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ class MissingType(object):
88
from django.contrib.postgres.fields import (
99
ArrayField,
1010
HStoreField,
11-
JSONField,
11+
JSONField as PGJSONField,
1212
RangeField,
1313
)
1414
except ImportError:
15-
ArrayField, HStoreField, JSONField, RangeField = (MissingType,) * 4
15+
ArrayField, HStoreField, PGJSONField, RangeField = (MissingType,) * 4
16+
17+
try:
18+
# JSONField is only available from Django 3.1
19+
from django.contrib.fields import JSONField
20+
except ImportError:
21+
JSONField = MissingType

graphene_django/converter.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from graphql import assert_valid_name
2525

2626
from .settings import graphene_settings
27-
from .compat import ArrayField, HStoreField, JSONField, RangeField
27+
from .compat import ArrayField, HStoreField, JSONField, PGJSONField, RangeField
2828
from .fields import DjangoListField, DjangoConnectionField
2929
from .utils import import_single_dispatch
3030
from .utils.str_converters import to_const
@@ -267,8 +267,9 @@ def convert_postgres_array_to_list(field, registry=None):
267267

268268

269269
@convert_django_field.register(HStoreField)
270+
@convert_django_field.register(PGJSONField)
270271
@convert_django_field.register(JSONField)
271-
def convert_postgres_field_to_string(field, registry=None):
272+
def convert_pg_and_json_field_to_string(field, registry=None):
272273
return JSONString(description=field.help_text, required=not field.null)
273274

274275

graphene_django/tests/test_converter.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
from graphene.types.datetime import Date, DateTime, Time
1212
from graphene.types.json import JSONString
1313

14-
from ..compat import ArrayField, HStoreField, JSONField, MissingType, RangeField
14+
from ..compat import (
15+
ArrayField,
16+
HStoreField,
17+
JSONField,
18+
PGJSONField,
19+
MissingType,
20+
RangeField,
21+
)
1522
from ..converter import (
1623
convert_django_field,
1724
convert_django_field_with_choices,
@@ -348,8 +355,13 @@ def test_should_postgres_hstore_convert_string():
348355
assert_conversion(HStoreField, JSONString)
349356

350357

351-
@pytest.mark.skipif(JSONField is MissingType, reason="JSONField should exist")
358+
@pytest.mark.skipif(PGJSONField is MissingType, reason="PGJSONField should exist")
352359
def test_should_postgres_json_convert_string():
360+
assert_conversion(PGJSONField, JSONString)
361+
362+
363+
@pytest.mark.skipif(JSONField is MissingType, reason="JSONField should exist")
364+
def test_should_json_convert_string():
353365
assert_conversion(JSONField, JSONString)
354366

355367

tox.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[tox]
22
envlist =
33
py{27,35,36,37,38}-django{111,20,21,22,master},
4-
py{36,37,38}-django30,
4+
py{36,37,38}-django{30,31},
55
black,flake8
66

77
[gh-actions]
@@ -18,6 +18,7 @@ DJANGO =
1818
2.1: django21
1919
2.2: django22
2020
3.0: django30
21+
3.1: django31
2122
master: djangomaster
2223

2324
[testenv]
@@ -33,6 +34,7 @@ deps =
3334
django21: Django>=2.1,<2.2
3435
django22: Django>=2.2,<3.0
3536
django30: Django>=3.0a1,<3.1
37+
django31: Django>=3.1,<3.2
3638
djangomaster: https://github.com/django/django/archive/master.zip
3739
commands = {posargs:py.test --cov=graphene_django graphene_django examples}
3840

0 commit comments

Comments
 (0)