BUGFIX: don't filter out lookup_field as input (required for update) (#1029)

This commit is contained in:
CBuiVNG 2020-10-19 18:25:35 +02:00 committed by GitHub
parent ee3d4f521f
commit 65f41c1a17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View File

@ -26,6 +26,7 @@ def fields_for_serializer(
exclude_fields,
is_input=False,
convert_choices_to_enum=True,
lookup_field=None,
):
fields = OrderedDict()
for name, field in serializer.fields.items():
@ -35,7 +36,9 @@ def fields_for_serializer(
name in exclude_fields,
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
field.read_only
and is_input
and lookup_field != name, # don't show read_only fields in Input
]
)
@ -91,6 +94,7 @@ class SerializerMutation(ClientIDMutation):
exclude_fields,
is_input=True,
convert_choices_to_enum=convert_choices_to_enum,
lookup_field=lookup_field,
)
output_fields = fields_for_serializer(
serializer,
@ -98,6 +102,7 @@ class SerializerMutation(ClientIDMutation):
exclude_fields,
is_input=False,
convert_choices_to_enum=convert_choices_to_enum,
lookup_field=lookup_field,
)
if not _meta:

View File

@ -143,17 +143,20 @@ def test_write_only_field_using_extra_kwargs():
def test_read_only_fields():
class ReadOnlyFieldModelSerializer(serializers.ModelSerializer):
id = serializers.CharField(read_only=True)
cool_name = serializers.CharField(read_only=True)
class Meta:
model = MyFakeModelWithPassword
fields = ["cool_name", "password"]
lookup_field = "id"
fields = ["id", "cool_name", "password"]
class MyMutation(SerializerMutation):
class Meta:
serializer_class = ReadOnlyFieldModelSerializer
assert "password" in MyMutation.Input._meta.fields
assert "id" 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"