Skip to content

Commit 7476576

Browse files
committed
Add all fields option
1 parent f7d0519 commit 7476576

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

graphene_django/tests/test_types.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,27 @@ class Meta:
242242
fields = ("id", "email", "films")
243243

244244

245+
@with_local_registry
246+
def test_django_objecttype_all_fields():
247+
class Reporter(DjangoObjectType):
248+
class Meta:
249+
model = ReporterModel
250+
fields = "__all__"
251+
252+
fields = list(Reporter._meta.fields.keys())
253+
assert fields == [
254+
"id",
255+
"first_name",
256+
"last_name",
257+
"email",
258+
"pets",
259+
"a_choice",
260+
"reporter_type",
261+
"films",
262+
"articles",
263+
]
264+
265+
245266
@with_local_registry
246267
def test_django_objecttype_exclude_fields():
247268
class Reporter(DjangoObjectType):

graphene_django/types.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
from typing import Type
2525

2626

27+
ALL_FIELDS = "__all__"
28+
29+
2730
def construct_fields(
2831
model, registry, only_fields, exclude_fields, convert_choices_to_enum
2932
):
@@ -121,12 +124,15 @@ def __init_subclass_with_meta__(
121124
raise Exception("Can't set both only_fields and fields")
122125
if only_fields:
123126
fields = only_fields
124-
if fields and not isinstance(fields, (list, tuple)):
127+
if fields and fields != ALL_FIELDS and not isinstance(fields, (list, tuple)):
125128
raise TypeError(
126129
'The `fields` option must be a list or tuple or "__all__". '
127130
"Got %s." % type(fields).__name__
128131
)
129132

133+
if fields == ALL_FIELDS:
134+
fields = None
135+
130136
# Alias exclude_fields -> exclude
131137
if exclude_fields and exclude:
132138
raise Exception("Can't set both exclude_fields and exclude")

0 commit comments

Comments
 (0)