Skip to content

use to_representation in favor of get_attribute #848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions graphene_django/rest_framework/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ class MyFakeModel(models.Model):
class MyFakeModelWithPassword(models.Model):
cool_name = models.CharField(max_length=50)
password = models.CharField(max_length=50)


class MyFakeModelWithDate(models.Model):
cool_name = models.CharField(max_length=50)
last_edited = models.DateField()
6 changes: 5 additions & 1 deletion graphene_django/rest_framework/mutation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import OrderedDict

from django.shortcuts import get_object_or_404
from rest_framework import serializers

import graphene
from graphene.relay.mutation import ClientIDMutation
Expand Down Expand Up @@ -141,6 +142,9 @@ def perform_mutate(cls, serializer, info):
kwargs = {}
for f, field in serializer.fields.items():
if not field.write_only:
kwargs[f] = field.get_attribute(obj)
if isinstance(field, serializers.SerializerMethodField):
kwargs[f] = field.to_representation(obj)
else:
kwargs[f] = field.get_attribute(obj)

return cls(errors=None, **kwargs)
31 changes: 30 additions & 1 deletion graphene_django/rest_framework/tests/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ...settings import graphene_settings
from ...types import DjangoObjectType
from ..models import MyFakeModel, MyFakeModelWithPassword
from ..models import MyFakeModel, MyFakeModelWithPassword, MyFakeModelWithDate
from ..mutation import SerializerMutation


Expand All @@ -33,6 +33,18 @@ class Meta:
fields = "__all__"


class MyModelSerializerWithMethod(serializers.ModelSerializer):
days_since_last_edit = serializers.SerializerMethodField()

class Meta:
model = MyFakeModelWithDate
fields = "__all__"

def get_days_since_last_edit(self, obj):
now = datetime.date(2020, 1, 8)
return (now - obj.last_edited).days


class MyModelMutation(SerializerMutation):
class Meta:
serializer_class = MyModelSerializer
Expand Down Expand Up @@ -208,6 +220,23 @@ class Meta:
assert '"id" required' in str(exc.value)


@mark.django_db
def test_perform_mutate_success():
class MyMethodMutation(SerializerMutation):
class Meta:
serializer_class = MyModelSerializerWithMethod

result = MyMethodMutation.mutate_and_get_payload(
None,
mock_info(),
**{"cool_name": "Narf", "last_edited": datetime.date(2020, 1, 4)}
)

assert result.errors is None
assert result.cool_name == "Narf"
assert result.days_since_last_edit == 4


def test_mutate_and_get_payload_error():
class MyMutation(SerializerMutation):
class Meta:
Expand Down