Skip to content

Commit 01b7ef6

Browse files
committed
Allow defining fields as an empty list
1 parent f3f0608 commit 01b7ef6

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

graphene_django/tests/test_types.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,17 @@ class Meta:
233233
assert fields == ["id", "email", "films"]
234234

235235

236+
@with_local_registry
237+
def test_django_objecttype_fields_empty():
238+
class Reporter(DjangoObjectType):
239+
class Meta:
240+
model = ReporterModel
241+
fields = ()
242+
243+
fields = list(Reporter._meta.fields.keys())
244+
assert fields == []
245+
246+
236247
@with_local_registry
237248
def test_django_objecttype_only_fields_and_fields():
238249
with pytest.raises(Exception):

graphene_django/types.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,15 @@ def construct_fields(
3535

3636
fields = OrderedDict()
3737
for name, field in _model_fields:
38-
is_not_in_only = only_fields and name not in only_fields
38+
is_not_in_only = (
39+
only_fields is not None
40+
and only_fields != ALL_FIELDS
41+
and name not in only_fields
42+
)
3943
# is_already_created = name in options.fields
40-
is_excluded = name in exclude_fields # or is_already_created
44+
is_excluded = (
45+
exclude_fields is not None and name in exclude_fields
46+
) # or is_already_created
4147
# https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.ForeignKey.related_query_name
4248
is_no_backref = str(name).endswith("+")
4349
if is_not_in_only or is_excluded or is_no_backref:
@@ -65,6 +71,7 @@ def construct_fields(
6571
def validate_fields(type_, model, fields, only_fields, exclude_fields):
6672
# Validate the given fields against the model's fields and custom fields
6773
all_field_names = set(fields.keys())
74+
only_fields = only_fields if only_fields is not ALL_FIELDS else ()
6875
for fields_list in (only_fields, exclude_fields):
6976
if not fields_list:
7077
continue
@@ -116,10 +123,10 @@ def __init_subclass_with_meta__(
116123
model=None,
117124
registry=None,
118125
skip_registry=False,
119-
only_fields=(), # deprecated in favour of `fields`
120-
fields=(),
121-
exclude_fields=(), # deprecated in favour of `exclude`
122-
exclude=(),
126+
only_fields=None, # deprecated in favour of `fields`
127+
fields=None,
128+
exclude_fields=None, # deprecated in favour of `exclude`
129+
exclude=None,
123130
filter_fields=None,
124131
filterset_class=None,
125132
connection=None,
@@ -174,9 +181,6 @@ def __init_subclass_with_meta__(
174181
"Got %s." % type(fields).__name__
175182
)
176183

177-
if fields == ALL_FIELDS:
178-
fields = None
179-
180184
# Alias exclude_fields -> exclude
181185
if exclude_fields and exclude:
182186
raise Exception("Can't set both exclude_fields and exclude")

0 commit comments

Comments
 (0)