From 30306e8108d5a3a3f138935aee40dcb3465d67ca Mon Sep 17 00:00:00 2001 From: "radoslaw.kowalski" Date: Sun, 7 Jun 2020 12:08:41 +0200 Subject: [PATCH] Warn when DjangoObjectType has neither fields nor exclude option --- graphene_django/tests/test_types.py | 31 +++++++++++++++++++++++++++++ graphene_django/types.py | 10 ++++++++++ 2 files changed, 41 insertions(+) diff --git a/graphene_django/tests/test_types.py b/graphene_django/tests/test_types.py index 1986568..9811d48 100644 --- a/graphene_django/tests/test_types.py +++ b/graphene_django/tests/test_types.py @@ -476,6 +476,37 @@ def test_django_objecttype_exclude_fields_exist_on_model(): assert len(record) == 0 +@with_local_registry +def test_django_objecttype_neither_fields_nor_exclude(): + with pytest.warns( + PendingDeprecationWarning, + match=r"Creating a DjangoObjectType without either the `fields` " + "or the `exclude` option is deprecated.", + ): + + class Reporter(DjangoObjectType): + class Meta: + model = ReporterModel + + with pytest.warns(None) as record: + + class Reporter2(DjangoObjectType): + class Meta: + model = ReporterModel + fields = ["email"] + + assert len(record) == 0 + + with pytest.warns(None) as record: + + class Reporter3(DjangoObjectType): + class Meta: + model = ReporterModel + exclude = ["email"] + + assert len(record) == 0 + + def custom_enum_name(field): return "CustomEnum{}".format(field.name.title()) diff --git a/graphene_django/types.py b/graphene_django/types.py index f389421..dcfbbdb 100644 --- a/graphene_django/types.py +++ b/graphene_django/types.py @@ -220,6 +220,16 @@ class DjangoObjectType(ObjectType): % type(exclude).__name__ ) + if fields is None and exclude is None: + warnings.warn( + "Creating a DjangoObjectType without either the `fields` " + "or the `exclude` option is deprecated. Add an explicit `fields " + "= '__all__'` option on DjangoObjectType {class_name} to use all " + "fields".format(class_name=cls.__name__,), + PendingDeprecationWarning, + stacklevel=2, + ) + django_fields = yank_fields_from_attrs( construct_fields(model, registry, fields, exclude, convert_choices_to_enum), _as=Field,