Skip to content

Allow defining fields as an empty list #871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions graphene_django/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ class Meta:
assert fields == ["id", "email", "films"]


@with_local_registry
def test_django_objecttype_fields_empty():
class Reporter(DjangoObjectType):
class Meta:
model = ReporterModel
fields = ()

fields = list(Reporter._meta.fields.keys())
assert fields == []


@with_local_registry
def test_django_objecttype_only_fields_and_fields():
with pytest.raises(Exception):
Expand Down
22 changes: 13 additions & 9 deletions graphene_django/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ def construct_fields(

fields = OrderedDict()
for name, field in _model_fields:
is_not_in_only = only_fields and name not in only_fields
is_not_in_only = (
only_fields is not None
and only_fields != ALL_FIELDS
and name not in only_fields
)
# is_already_created = name in options.fields
is_excluded = name in exclude_fields # or is_already_created
is_excluded = (
exclude_fields is not None and name in exclude_fields
) # or is_already_created
# https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.ForeignKey.related_query_name
is_no_backref = str(name).endswith("+")
if is_not_in_only or is_excluded or is_no_backref:
Expand Down Expand Up @@ -65,6 +71,7 @@ def construct_fields(
def validate_fields(type_, model, fields, only_fields, exclude_fields):
# Validate the given fields against the model's fields and custom fields
all_field_names = set(fields.keys())
only_fields = only_fields if only_fields is not ALL_FIELDS else ()
for name in only_fields or ():
if name in all_field_names:
continue
Expand Down Expand Up @@ -142,10 +149,10 @@ def __init_subclass_with_meta__(
model=None,
registry=None,
skip_registry=False,
only_fields=(), # deprecated in favour of `fields`
fields=(),
exclude_fields=(), # deprecated in favour of `exclude`
exclude=(),
only_fields=None, # deprecated in favour of `fields`
fields=None,
exclude_fields=None, # deprecated in favour of `exclude`
exclude=None,
filter_fields=None,
filterset_class=None,
connection=None,
Expand Down Expand Up @@ -200,9 +207,6 @@ def __init_subclass_with_meta__(
"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")
Expand Down