Skip to content

Commit 2acd869

Browse files
committed
Improve imports if not django_filters installed. Improved Django command fixing warning in Django 1.8+
1 parent 20a493b commit 2acd869

File tree

4 files changed

+53
-43
lines changed

4 files changed

+53
-43
lines changed

graphene/contrib/django/fields.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from ...core.types.definitions import List
55
from ...relay import ConnectionField
66
from ...relay.utils import is_node
7-
from .utils import get_type_for_model, maybe_queryset
7+
from .utils import get_type_for_model, maybe_queryset, DJANGO_FILTER_INSTALLED
88

99

1010
class DjangoConnectionField(ConnectionField):
@@ -37,7 +37,8 @@ def from_list(self, connection_type, resolved, args, info):
3737
class ConnectionOrListField(Field):
3838

3939
def internal_type(self, schema):
40-
from .filter.fields import DjangoFilterConnectionField
40+
if DJANGO_FILTER_INSTALLED:
41+
from .filter.fields import DjangoFilterConnectionField
4142

4243
model_field = self.type
4344
field_object_type = model_field.get_object_type(schema)
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import warnings
12
from graphene.contrib.django.utils import DJANGO_FILTER_INSTALLED
23

34
if not DJANGO_FILTER_INSTALLED:
4-
raise Exception(
5+
warnings.warn(
56
"Use of django filtering requires the django-filter package "
6-
"be installed. You can do so using `pip install django-filter`"
7+
"be installed. You can do so using `pip install django-filter`", ImportWarning
78
)
9+
else:
10+
from .fields import DjangoFilterConnectionField
11+
from .filterset import GrapheneFilterSet, GlobalIDFilter, GlobalIDMultipleChoiceFilter
812

9-
from .fields import DjangoFilterConnectionField
10-
from .filterset import GrapheneFilterSet, GlobalIDFilter, GlobalIDMultipleChoiceFilter
11-
12-
__all__ = ['DjangoFilterConnectionField', 'GrapheneFilterSet',
13-
'GlobalIDFilter', 'GlobalIDMultipleChoiceFilter']
13+
__all__ = ['DjangoFilterConnectionField', 'GrapheneFilterSet',
14+
'GlobalIDFilter', 'GlobalIDMultipleChoiceFilter']

graphene/contrib/django/management/commands/graphql_schema.py

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,53 @@
11
import importlib
22
import json
33
from optparse import make_option
4+
from distutils.version import StrictVersion
45

6+
from django import get_version as get_django_version
57
from django.core.management.base import BaseCommand, CommandError
68

9+
LT_DJANGO_1_8 = StrictVersion(get_django_version()) < StrictVersion('1.8')
710

8-
class Command(BaseCommand):
9-
help = 'Dump Graphene schema JSON to file'
10-
can_import_settings = True
11+
if LT_DJANGO_1_8:
12+
class CommandArguments(BaseCommand):
13+
option_list = BaseCommand.option_list + (
14+
make_option(
15+
'--schema',
16+
type=str,
17+
dest='schema',
18+
default='',
19+
help='Django app containing schema to dump, e.g. myproject.core.schema',
20+
),
21+
make_option(
22+
'--out',
23+
type=str,
24+
dest='out',
25+
default='',
26+
help='Output file (default: schema.json)'
27+
),
28+
)
29+
else:
30+
class CommandArguments(BaseCommand):
31+
def add_arguments(self, parser):
32+
from django.conf import settings
33+
parser.add_argument(
34+
'--schema',
35+
type=str,
36+
dest='schema',
37+
default=getattr(settings, 'GRAPHENE_SCHEMA', ''),
38+
help='Django app containing schema to dump, e.g. myproject.core.schema')
1139

12-
option_list = BaseCommand.option_list + (
13-
make_option(
14-
'--schema',
15-
type=str,
16-
dest='schema',
17-
default='',
18-
help='Django app containing schema to dump, e.g. myproject.core.schema',
19-
),
20-
make_option(
21-
'--out',
22-
type=str,
23-
dest='out',
24-
default='',
25-
help='Output file (default: schema.json)'
26-
),
27-
)
40+
parser.add_argument(
41+
'--out',
42+
type=str,
43+
dest='out',
44+
default=getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json'),
45+
help='Output file (default: schema.json)')
2846

29-
def add_arguments(self, parser):
30-
from django.conf import settings
31-
parser.add_argument(
32-
'--schema',
33-
type=str,
34-
dest='schema',
35-
default=getattr(settings, 'GRAPHENE_SCHEMA', ''),
36-
help='Django app containing schema to dump, e.g. myproject.core.schema')
3747

38-
parser.add_argument(
39-
'--out',
40-
type=str,
41-
dest='out',
42-
default=getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json'),
43-
help='Output file (default: schema.json)')
48+
class Command(CommandArguments):
49+
help = 'Dump Graphene schema JSON to file'
50+
can_import_settings = True
4451

4552
def save_file(self, out, schema_dict):
4653
with open(out, 'w') as outfile:

graphene/contrib/django/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
try:
1010
import django_filters # noqa
1111
DJANGO_FILTER_INSTALLED = True
12-
except ImportError:
12+
except (ImportError, AttributeError):
13+
# AtributeError raised if DjangoFilters installed with a incompatible Django Version
1314
DJANGO_FILTER_INSTALLED = False
1415

1516

0 commit comments

Comments
 (0)