Skip to content

get_related_resource_type returns parent model name #77

Closed
@martinmaillard

Description

@martinmaillard

This is a simplified version of my API:

class Survey(models.Model):
    pass

class Question(models.Model):
    survey = models.ForeignKey(Survey, related_name='questions')

class SurveySerializer(serializers.ModelSerializer):
    class Meta:
        model = Survey
        fields = ('id', 'questions')
        read_only_fields = ('questions',)

class SurveyViewSet(viewsets.ModelViewSet):
     queryset = Survey.objects.all()
     serializer_class = SurveySerializer

Now if I request a survey, the type of the questions is not correct :

>>> GET '/surveys/1'
{
    'data': {
        'id': '1',
        'type': 'surveys',
        'attributes': {},
        'relationships': {
            'questions': {
                'data': [
                    {
                        'id': '1',
                        'type': 'surveys',
                        'attributes': {},
                    },
                ],
            },
        },
    },
}

The issue seems to be in utils.get_related_resource_type. I would submit a PR, but I'm not entirely sure how to fix it without breaking something else...

Also, I'm guessing that it will be necessary to be able to define a resource_name for related models and the relationships handling code will change, but this is still a big issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions