Skip to content

Allow partial update with polymorphic serializers #655

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
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ Jason Housley <[email protected]>
Beni Keller <[email protected]>
Stas S. <[email protected]>
Nathanael Gordon <[email protected]>
Charlie Allatson <[email protected]>
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ any parts of the framework not mentioned in the documentation should generally b
* Don't swallow `filter[]` params when there are several
* Fix DeprecationWarning regarding collections.abc import in Python 3.7
* Allow OPTIONS request to be used on RelationshipView
* Avoid raising validation error for missing fields on a PATCH
request for polymorphic serializers

### Deprecated

Expand Down
18 changes: 18 additions & 0 deletions example/migrations/0007_artproject_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.2 on 2019-06-07 06:46

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('example', '0006_auto_20181228_0752'),
]

operations = [
migrations.AddField(
model_name='artproject',
name='description',
field=models.CharField(max_length=100, null=True),
),
]
1 change: 1 addition & 0 deletions example/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class Project(PolymorphicModel):

class ArtProject(Project):
artist = models.CharField(max_length=30)
description = models.CharField(max_length=100, null=True)


class ResearchProject(Project):
Expand Down
17 changes: 17 additions & 0 deletions example/tests/integration/test_polymorphism.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest
from django.urls import reverse
from rest_framework import status

from example.factories import ArtProjectFactory, ProjectTypeFactory

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


def test_patch_on_polymorphic_model_without_including_required_field(single_art_project, client):
url = reverse("project-detail", kwargs={'pk': single_art_project.pk})
data = {
'data': {
'id': single_art_project.pk,
'type': 'artProjects',
'attributes': {
'description': 'New description'
}
}
}
response = client.patch(url, data)
assert response.status_code == status.HTTP_200_OK
assert response.json()['data']['attributes']['description'] == 'New description'


def test_polymorphism_on_polymorphic_model_list_post(client):
test_topic = 'New test topic {}'.format(random.randint(0, 999999))
test_artist = 'test-{}'.format(random.randint(0, 999999))
Expand Down
3 changes: 2 additions & 1 deletion rest_framework_json_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,5 @@ def to_internal_value(self, data):
expected_types=', '.join(expected_types), received_type=received_type))
serializer_class = self.get_polymorphic_serializer_for_type(received_type)
self.__class__ = serializer_class
return serializer_class(data, context=self.context).to_internal_value(data)
return serializer_class(data, context=self.context,
partial=self.partial).to_internal_value(data)