Skip to content

Commit 21600c6

Browse files
charlieallatsonsliverc
authored andcommitted
Allow partial update with polymorphic serializers (#655)
1 parent 32a9d11 commit 21600c6

File tree

6 files changed

+41
-1
lines changed

6 files changed

+41
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ Jason Housley <[email protected]>
2424
Beni Keller <[email protected]>
2525
2626
Nathanael Gordon <[email protected]>
27+
Charlie Allatson <[email protected]>

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ any parts of the framework not mentioned in the documentation should generally b
2626
* Don't swallow `filter[]` params when there are several
2727
* Fix DeprecationWarning regarding collections.abc import in Python 3.7
2828
* Allow OPTIONS request to be used on RelationshipView
29+
* Avoid raising validation error for missing fields on a PATCH
30+
request for polymorphic serializers
2931

3032
### Deprecated
3133

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 2.2.2 on 2019-06-07 06:46
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('example', '0006_auto_20181228_0752'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='artproject',
15+
name='description',
16+
field=models.CharField(max_length=100, null=True),
17+
),
18+
]

example/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ class Project(PolymorphicModel):
153153

154154
class ArtProject(Project):
155155
artist = models.CharField(max_length=30)
156+
description = models.CharField(max_length=100, null=True)
156157

157158

158159
class ResearchProject(Project):

example/tests/integration/test_polymorphism.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44
from django.urls import reverse
5+
from rest_framework import status
56

67
from example.factories import ArtProjectFactory, ProjectTypeFactory
78

@@ -59,6 +60,22 @@ def test_polymorphism_on_polymorphic_model_detail_patch(single_art_project, clie
5960
assert new_content['data']['attributes']['artist'] == test_artist
6061

6162

63+
def test_patch_on_polymorphic_model_without_including_required_field(single_art_project, client):
64+
url = reverse("project-detail", kwargs={'pk': single_art_project.pk})
65+
data = {
66+
'data': {
67+
'id': single_art_project.pk,
68+
'type': 'artProjects',
69+
'attributes': {
70+
'description': 'New description'
71+
}
72+
}
73+
}
74+
response = client.patch(url, data)
75+
assert response.status_code == status.HTTP_200_OK
76+
assert response.json()['data']['attributes']['description'] == 'New description'
77+
78+
6279
def test_polymorphism_on_polymorphic_model_list_post(client):
6380
test_topic = 'New test topic {}'.format(random.randint(0, 999999))
6481
test_artist = 'test-{}'.format(random.randint(0, 999999))

rest_framework_json_api/serializers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,5 @@ def to_internal_value(self, data):
353353
expected_types=', '.join(expected_types), received_type=received_type))
354354
serializer_class = self.get_polymorphic_serializer_for_type(received_type)
355355
self.__class__ = serializer_class
356-
return serializer_class(data, context=self.context).to_internal_value(data)
356+
return serializer_class(data, context=self.context,
357+
partial=self.partial).to_internal_value(data)

0 commit comments

Comments
 (0)