Skip to content

Commit

Permalink
Allow defining fields as an empty list
Browse files Browse the repository at this point in the history
  • Loading branch information
jkimbo committed Jan 30, 2020
1 parent 47650e7 commit b87ffa3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
11 changes: 11 additions & 0 deletions graphene_django/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,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(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 fields_list in (only_fields, exclude_fields):
if not fields_list:
continue
Expand Down Expand Up @@ -103,10 +110,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 @@ -161,9 +168,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

0 comments on commit b87ffa3

Please sign in to comment.