Exclude read_only fields from input (#882)

This commit is contained in:
Amim Knabben 2020-02-23 04:48:33 -05:00 committed by GitHub
parent 7a14b77a41
commit a12fc9299a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 6 deletions

View File

@ -30,12 +30,13 @@ def fields_for_serializer(
fields = OrderedDict() fields = OrderedDict()
for name, field in serializer.fields.items(): for name, field in serializer.fields.items():
is_not_in_only = only_fields and name not in only_fields is_not_in_only = only_fields and name not in only_fields
is_excluded = ( is_excluded = any(
name [
in exclude_fields # or name in exclude_fields,
# name in already_created_fields field.write_only
) or ( and not is_input, # don't show write_only fields in Query
field.write_only and not is_input # don't show write_only fields in Query field.read_only and is_input, # don't show read_only fields in Input
]
) )
if is_not_in_only or is_excluded: if is_not_in_only or is_excluded:

View File

@ -144,6 +144,25 @@ def test_write_only_field_using_extra_kwargs():
), "'password' is write_only field and shouldn't be visible" ), "'password' is write_only field and shouldn't be visible"
@mark.django_db
def test_read_only_fields():
class ReadOnlyFieldModelSerializer(serializers.ModelSerializer):
cool_name = serializers.CharField(read_only=True)
class Meta:
model = MyFakeModelWithPassword
fields = ["cool_name", "password"]
class MyMutation(SerializerMutation):
class Meta:
serializer_class = ReadOnlyFieldModelSerializer
assert "password" in MyMutation.Input._meta.fields
assert (
"cool_name" not in MyMutation.Input._meta.fields
), "'cool_name' is read_only field and shouldn't be on arguments"
def test_nested_model(): def test_nested_model():
class MyFakeModelGrapheneType(DjangoObjectType): class MyFakeModelGrapheneType(DjangoObjectType):
class Meta: class Meta: