Skip to content

Commit 28900c5

Browse files
committed
modelserializer docs
1 parent 4cd79a9 commit 28900c5

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

docs/rest-framework.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,51 @@ You can create a Mutation based on a serializer by using the
1919
class Meta:
2020
serializer_class = MySerializer
2121
22+
Add/Update Operations
23+
---------------------
24+
25+
By default ModelSerializers accept add and update operations. To
26+
customize this use the `model_operations` attribute. The update
27+
operation looks up models by the primary key by default. You can
28+
customize the look up with the lookup attribute.
29+
30+
Other default attributes:
31+
32+
`partial = False`: Accept updates without all the fields.
33+
34+
.. code:: python
35+
36+
from graphene_django.rest_framework.mutation import SerializerMutation
37+
38+
class MyAwesomeMutation(SerializerMutation):
39+
class Meta:
40+
serializer_class = MySerializer
41+
model_operations = ['add', 'update']
42+
lookup_field = 'id'
43+
partial = False
44+
45+
Overriding Update Look Ups
46+
--------------------------
47+
48+
Use the method `resolve_serializer_inputs` to override how
49+
updates are queried.
50+
51+
.. code:: python
52+
53+
from graphene_django.rest_framework.mutation import SerializerMutation
54+
55+
class MyAwesomeMutation(SerializerMutation):
56+
class Meta:
57+
serializer_class = MySerializer
58+
59+
@classmethod
60+
def resolve_serializer_inputs(cls, root, info, **input):
61+
if 'id' in input:
62+
instance = Post.objects.filter(id=id, owner=info.context.user).first()
63+
if instance:
64+
return {'intance': instance, 'data': input, 'partial': True}
65+
66+
else:
67+
raise http.Http404
68+
69+
return {'data': input, 'partial': True}

0 commit comments

Comments
 (0)