diff --git a/graphene_django/tests/test_types.py b/graphene_django/tests/test_types.py index 0cd0740..c74a6b9 100644 --- a/graphene_django/tests/test_types.py +++ b/graphene_django/tests/test_types.py @@ -242,6 +242,27 @@ def test_django_objecttype_only_fields_and_fields(): fields = ("id", "email", "films") +@with_local_registry +def test_django_objecttype_all_fields(): + class Reporter(DjangoObjectType): + class Meta: + model = ReporterModel + fields = "__all__" + + fields = list(Reporter._meta.fields.keys()) + assert fields == [ + "id", + "first_name", + "last_name", + "email", + "pets", + "a_choice", + "reporter_type", + "films", + "articles", + ] + + @with_local_registry def test_django_objecttype_exclude_fields(): class Reporter(DjangoObjectType): diff --git a/graphene_django/types.py b/graphene_django/types.py index 145e9a9..855761c 100644 --- a/graphene_django/types.py +++ b/graphene_django/types.py @@ -24,6 +24,9 @@ if six.PY3: from typing import Type +ALL_FIELDS = "__all__" + + def construct_fields( model, registry, only_fields, exclude_fields, convert_choices_to_enum ): @@ -121,12 +124,15 @@ class DjangoObjectType(ObjectType): raise Exception("Can't set both only_fields and fields") if only_fields: fields = only_fields - if fields and not isinstance(fields, (list, tuple)): + if fields and fields != ALL_FIELDS and not isinstance(fields, (list, tuple)): raise TypeError( 'The `fields` option must be a list or tuple or "__all__". ' "Got %s." % type(fields).__name__ ) + if fields == ALL_FIELDS: + fields = None + # Alias exclude_fields -> exclude if exclude_fields and exclude: raise Exception("Can't set both exclude_fields and exclude")