Add all fields option

This commit is contained in:
Jonathan Kim 2019-06-25 17:01:04 +01:00
parent a8584febad
commit 4c10afba88
2 changed files with 28 additions and 1 deletions

View File

@ -242,6 +242,27 @@ def test_django_objecttype_only_fields_and_fields():
fields = ("id", "email", "films") 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 @with_local_registry
def test_django_objecttype_exclude_fields(): def test_django_objecttype_exclude_fields():
class Reporter(DjangoObjectType): class Reporter(DjangoObjectType):

View File

@ -24,6 +24,9 @@ if six.PY3:
from typing import Type from typing import Type
ALL_FIELDS = "__all__"
def construct_fields( def construct_fields(
model, registry, only_fields, exclude_fields, convert_choices_to_enum 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") raise Exception("Can't set both only_fields and fields")
if only_fields: if only_fields:
fields = 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( raise TypeError(
'The `fields` option must be a list or tuple or "__all__". ' 'The `fields` option must be a list or tuple or "__all__". '
"Got %s." % type(fields).__name__ "Got %s." % type(fields).__name__
) )
if fields == ALL_FIELDS:
fields = None
# Alias exclude_fields -> exclude # Alias exclude_fields -> exclude
if exclude_fields and exclude: if exclude_fields and exclude:
raise Exception("Can't set both exclude_fields and exclude") raise Exception("Can't set both exclude_fields and exclude")