Skip to content

Fix related resource on inherited polymorphic model #767

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 @@ -27,3 +27,4 @@ Stas S. <stas@nerd.ro>
Nathanael Gordon <nathanael.l.gordon@gmail.com>
Charlie Allatson <charles.allatson@gmail.com>
Joseba Mendivil <git@jma.email>
Felix Viernickel <felix@gedankenspieler.org>
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ any parts of the framework not mentioned in the documentation should generally b
* Ensure that `409 Conflict` is returned when processing a `PATCH` request in which the resource object’s type and id do not match the server’s endpoint properly as outlined in [JSON:API](https://jsonapi.org/format/#crud-updating-responses-409) spec.
* Properly return parser error when primary data is of invalid type
* Pass instance to child serializer when `PolymorphicModelSerializer` inits it in `to_internal_value`
* Handle serialization of related resources on inherited polymorphic models that are absent on the base model

## [3.0.0] - 2019-10-14

Expand Down
23 changes: 23 additions & 0 deletions example/migrations/0008_labresults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.0.3 on 2020-02-06 10:24

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('example', '0007_artproject_description'),
]

operations = [
migrations.CreateModel(
name='LabResults',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('date', models.DateField()),
('measurements', models.TextField()),
('research_project', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='lab_results', to='example.ResearchProject')),
],
),
]
7 changes: 7 additions & 0 deletions example/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ class ResearchProject(Project):
supervisor = models.CharField(max_length=30)


class LabResults(models.Model):
research_project = models.ForeignKey(
ResearchProject, related_name='lab_results', on_delete=models.CASCADE)
date = models.DateField()
measurements = models.TextField()


class Company(models.Model):
name = models.CharField(max_length=100)
current_project = models.ForeignKey(
Expand Down
10 changes: 10 additions & 0 deletions example/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Comment,
Company,
Entry,
LabResults,
Project,
ProjectType,
ResearchProject,
Expand Down Expand Up @@ -303,11 +304,20 @@ class Meta:


class ResearchProjectSerializer(BaseProjectSerializer):
# testing exclusive related field on inherited polymorphic model
lab_results = relations.ResourceRelatedField(many=True, read_only=True)

class Meta:
model = ResearchProject
exclude = ('polymorphic_ctype',)


class LabResultsSerializer(serializers.ModelSerializer):
class Meta:
model = LabResults
fields = ('date', 'measurements')


class ProjectSerializer(serializers.PolymorphicModelSerializer):
included_serializers = {
'project_type': ProjectTypeSerializer,
Expand Down
6 changes: 5 additions & 1 deletion rest_framework_json_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def format_resource_type(value, format_type=None, pluralize=None):


def get_related_resource_type(relation):
from rest_framework_json_api.serializers import PolymorphicModelSerializer
try:
return get_resource_type_from_serializer(relation)
except AttributeError:
Expand All @@ -165,7 +166,10 @@ def get_related_resource_type(relation):
else:
parent_serializer = relation.parent
parent_model = None
if hasattr(parent_serializer, 'Meta'):
if isinstance(parent_serializer, PolymorphicModelSerializer):
parent_model = parent_serializer.get_polymorphic_serializer_for_instance(
parent_serializer.instance).Meta.model
elif hasattr(parent_serializer, 'Meta'):
parent_model = getattr(parent_serializer.Meta, 'model', None)
elif hasattr(parent_serializer, 'parent') and hasattr(parent_serializer.parent, 'Meta'):
parent_model = getattr(parent_serializer.parent.Meta, 'model', None)
Expand Down