Skip to content

Commit c9386eb

Browse files
committed
Allow defining fields as an empty list
1 parent 8990e17 commit c9386eb

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
@@ -230,6 +230,17 @@ class Meta:
230230
assert fields == ["id", "email", "films"]
231231

232232

233+
@with_local_registry
234+
def test_django_objecttype_fields_empty():
235+
class Reporter(DjangoObjectType):
236+
class Meta:
237+
model = ReporterModel
238+
fields = ()
239+
240+
fields = list(Reporter._meta.fields.keys())
241+
assert fields == []
242+
243+
233244
@with_local_registry
234245
def test_django_objecttype_only_fields_and_fields():
235246
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 name in only_fields or ():
6976
if name in all_field_names:
7077
continue
@@ -142,10 +149,10 @@ def __init_subclass_with_meta__(
142149
model=None,
143150
registry=None,
144151
skip_registry=False,
145-
only_fields=(), # deprecated in favour of `fields`
146-
fields=(),
147-
exclude_fields=(), # deprecated in favour of `exclude`
148-
exclude=(),
152+
only_fields=None, # deprecated in favour of `fields`
153+
fields=None,
154+
exclude_fields=None, # deprecated in favour of `exclude`
155+
exclude=None,
149156
filter_fields=None,
150157
filterset_class=None,
151158
connection=None,
@@ -200,9 +207,6 @@ def __init_subclass_with_meta__(
200207
"Got %s." % type(fields).__name__
201208
)
202209

203-
if fields == ALL_FIELDS:
204-
fields = None
205-
206210
# Alias exclude_fields -> exclude
207211
if exclude_fields and exclude:
208212
raise Exception("Can't set both exclude_fields and exclude")

0 commit comments

Comments
 (0)