Skip to content

Commit

Permalink
Add check for serializers.HiddenField on fields_for_serializer fu…
Browse files Browse the repository at this point in the history
…nction (#1419)

* Add check for `serializers.HiddenField` on fields_for_serializer function

* Add pre-commit changes
  • Loading branch information
sezginacer authored Jun 6, 2023
1 parent 520ddea commit 8934393
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions graphene_django/rest_framework/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def fields_for_serializer(
field.read_only
and is_input
and lookup_field != name, # don't show read_only fields in Input
isinstance(
field, serializers.HiddenField
), # don't show hidden fields in Input
]
)

Expand Down
15 changes: 15 additions & 0 deletions graphene_django/rest_framework/tests/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,21 @@ class Meta:
), "'cool_name' is read_only field and shouldn't be on arguments"


def test_hidden_fields():
class SerializerWithHiddenField(serializers.Serializer):
cool_name = serializers.CharField()
user = serializers.HiddenField(default=serializers.CurrentUserDefault())

class MyMutation(SerializerMutation):
class Meta:
serializer_class = SerializerWithHiddenField

assert "cool_name" in MyMutation.Input._meta.fields
assert (
"user" not in MyMutation.Input._meta.fields
), "'user' is hidden field and shouldn't be on arguments"


def test_nested_model():
class MyFakeModelGrapheneType(DjangoObjectType):
class Meta:
Expand Down

0 comments on commit 8934393

Please sign in to comment.