mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-29 04:53:43 +03:00
Exclude read_only fields from input (#882)
This commit is contained in:
parent
7a14b77a41
commit
a12fc9299a
|
@ -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:
|
||||||
|
|
|
@ -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:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user