mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-13 09:42:32 +03:00
Add all fields option
This commit is contained in:
parent
a8584febad
commit
4c10afba88
|
@ -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):
|
||||||
|
|
|
@ -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")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user