Skip to content

Commit 51cbae3

Browse files
committed
Warn when DjangoObjectType has neither fields nor exclude option
1 parent c002034 commit 51cbae3

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

graphene_django/tests/test_types.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,37 @@ class Meta:
394394
assert len(record) == 0
395395

396396

397+
@with_local_registry
398+
def test_django_objecttype_neither_fields_nor_exclude():
399+
with pytest.warns(
400+
PendingDeprecationWarning,
401+
match=r"Creating a DjangoObjectType without either the `fields` "
402+
"or the `exclude` option is deprecated.",
403+
):
404+
405+
class Reporter(DjangoObjectType):
406+
class Meta:
407+
model = ReporterModel
408+
409+
with pytest.warns(None) as record:
410+
411+
class Reporter2(DjangoObjectType):
412+
class Meta:
413+
model = ReporterModel
414+
fields = ["email"]
415+
416+
assert len(record) == 0
417+
418+
with pytest.warns(None) as record:
419+
420+
class Reporter3(DjangoObjectType):
421+
class Meta:
422+
model = ReporterModel
423+
exclude = ["email"]
424+
425+
assert len(record) == 0
426+
427+
397428
def custom_enum_name(field):
398429
return "CustomEnum{}".format(field.name.title())
399430

graphene_django/types.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,16 @@ def __init_subclass_with_meta__(
223223
% type(exclude).__name__
224224
)
225225

226+
if fields is None and exclude is None:
227+
warnings.warn(
228+
"Creating a DjangoObjectType without either the `fields` "
229+
"or the `exclude` option is deprecated. Add an explicit `fields "
230+
"= '__all__'` option on DjangoObjectType {class_name} to use all "
231+
"fields".format(class_name=cls.__name__,),
232+
PendingDeprecationWarning,
233+
stacklevel=2,
234+
)
235+
226236
django_fields = yank_fields_from_attrs(
227237
construct_fields(model, registry, fields, exclude, convert_choices_to_enum),
228238
_as=Field,

0 commit comments

Comments
 (0)