Skip to content

Commit 9710ec2

Browse files
committed
Warning JSONField
1 parent b108627 commit 9710ec2

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

django_editorjs/fields.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import traceback
2-
3-
import django
1+
from django.core import checks
42
from django.db.models import Field
53
from django.forms import Textarea
64

@@ -12,12 +10,9 @@
1210
# pylint: disable=ungrouped-imports
1311
from django.db.models import JSONField
1412
except ImportError:
15-
pass
16-
17-
NEW_VERSION = False
18-
19-
if django.VERSION[0] == 3 and django.VERSION[1] >= 1:
20-
NEW_VERSION = True
13+
HAS_JSONFIELD = False
14+
else:
15+
HAS_JSONFIELD = True
2116

2217

2318
class FieldMixin(Field):
@@ -52,16 +47,22 @@ def __init__(self, plugins=None, tools=None, **kwargs):
5247
super().__init__(plugins, tools, **kwargs)
5348

5449

55-
class EditorJsJSONField(EditorJsFieldMixin, JSONField if NEW_VERSION else FieldMixin):
50+
class EditorJsJSONField(EditorJsFieldMixin, JSONField if HAS_JSONFIELD else FieldMixin):
5651
def __init__(self, plugins=None, tools=None, **kwargs):
57-
if not NEW_VERSION and DEBUG:
58-
print()
59-
print('\x1b[0;30;43m {}\x1b[0m'.format(
60-
'Warning: you don\'t support JSONField, ' +
61-
'please use EditorJsTextField instead of EditorJsJSONField'
62-
))
63-
print('\x1b[2;34;93m{}\x1b[0m'.format(
64-
traceback.format_stack()[-3:][0]
65-
))
66-
print()
6752
super().__init__(plugins, tools, **kwargs)
53+
54+
def check(self, **kwargs):
55+
errors = super().check(**kwargs)
56+
errors.extend(self._check_supported_json())
57+
return errors
58+
59+
def _check_supported_json(self):
60+
if not HAS_JSONFIELD and DEBUG:
61+
return [
62+
checks.Warning(
63+
'You don\'t support JSONField, please use'
64+
'EditorJsTextField instead of EditorJsJSONField',
65+
obj=self
66+
)
67+
]
68+
return []

0 commit comments

Comments
 (0)