Skip to content

Commit a12fc92

Browse files
authored
Exclude read_only fields from input (#882)
1 parent 7a14b77 commit a12fc92

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

graphene_django/rest_framework/mutation.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ def fields_for_serializer(
3030
fields = OrderedDict()
3131
for name, field in serializer.fields.items():
3232
is_not_in_only = only_fields and name not in only_fields
33-
is_excluded = (
34-
name
35-
in exclude_fields # or
36-
# name in already_created_fields
37-
) or (
38-
field.write_only and not is_input # don't show write_only fields in Query
33+
is_excluded = any(
34+
[
35+
name in exclude_fields,
36+
field.write_only
37+
and not is_input, # don't show write_only fields in Query
38+
field.read_only and is_input, # don't show read_only fields in Input
39+
]
3940
)
4041

4142
if is_not_in_only or is_excluded:

graphene_django/rest_framework/tests/test_mutation.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,25 @@ class Meta:
144144
), "'password' is write_only field and shouldn't be visible"
145145

146146

147+
@mark.django_db
148+
def test_read_only_fields():
149+
class ReadOnlyFieldModelSerializer(serializers.ModelSerializer):
150+
cool_name = serializers.CharField(read_only=True)
151+
152+
class Meta:
153+
model = MyFakeModelWithPassword
154+
fields = ["cool_name", "password"]
155+
156+
class MyMutation(SerializerMutation):
157+
class Meta:
158+
serializer_class = ReadOnlyFieldModelSerializer
159+
160+
assert "password" in MyMutation.Input._meta.fields
161+
assert (
162+
"cool_name" not in MyMutation.Input._meta.fields
163+
), "'cool_name' is read_only field and shouldn't be on arguments"
164+
165+
147166
def test_nested_model():
148167
class MyFakeModelGrapheneType(DjangoObjectType):
149168
class Meta:

0 commit comments

Comments
 (0)