Skip to content

Commit

Permalink
BUGFIX: don't filter out lookup_field as input (required for update) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
CBuiVNG authored Oct 19, 2020
1 parent ee3d4f5 commit 65f41c1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 6 additions & 1 deletion graphene_django/rest_framework/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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
]
)

Expand Down Expand Up @@ -91,13 +94,15 @@ def __init_subclass_with_meta__(
exclude_fields,
is_input=True,
convert_choices_to_enum=convert_choices_to_enum,
lookup_field=lookup_field,
)
output_fields = fields_for_serializer(
serializer,
only_fields,
exclude_fields,
is_input=False,
convert_choices_to_enum=convert_choices_to_enum,
lookup_field=lookup_field,
)

if not _meta:
Expand Down
5 changes: 4 additions & 1 deletion graphene_django/rest_framework/tests/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,20 @@ class Meta:

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"
Expand Down

0 comments on commit 65f41c1

Please sign in to comment.