Skip to content

get_related_resource_type returns parent model name #77

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

Closed
martinmaillard opened this issue Sep 14, 2015 · 14 comments
Closed

get_related_resource_type returns parent model name #77

martinmaillard opened this issue Sep 14, 2015 · 14 comments

Comments

@martinmaillard
Copy link
Contributor

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.

@jsenecal
Copy link
Member

Pull latest version from github, I fixed that I think in some commit.

@martinmaillard
Copy link
Contributor Author

Thanks for the quick reply! I upgraded to the latest commit on "develop" and the problem is still happening...

@jsenecal
Copy link
Member

Yes you are right, I couldn't see the whole picture from my cellphone. The get_related_resource_type method is hoping to be intelligent and tries to find the related name from the field name when everything else fails.

We are going to refactor this to be able to define a resouce_name as you suggested.

@jsenecal
Copy link
Member

Actually, the more I read it the more I think the behavior is the right one - Model 'survey' is referencing to the same model. so the relation model is in fact survey as well. Unless I misread your code...

Are you sure the output is the proper one? it doesn't make sense to have attributes where you put them under questions

@martinmaillard
Copy link
Contributor Author

The records in the questions relationship should have the type questions, not surveys.

@jsenecal
Copy link
Member

Oups - yes you are right - sry

@mkohram
Copy link

mkohram commented Sep 15, 2015

#74 should be merged with this.

jsenecal added a commit that referenced this issue Sep 16, 2015
jerel added a commit that referenced this issue Sep 16, 2015
Fix for issue #77. get_related_resource_type returns parent model name
@jerel
Copy link
Member

jerel commented Sep 16, 2015

This should be fixed by 64f8a6b, if it doesn't resolve your issue I'll reopen.

@jerel jerel closed this as completed Sep 16, 2015
@martinmaillard
Copy link
Contributor Author

The proper model is now used, but the first letter is a capital, which is probably not intended ?

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

@jsenecal
Copy link
Member

@martinmaillard I noticed that behavior as well, I think this was introduced in commit eeab460 altough I would like @schtibe to comment on this as he might have done this on purpose

@schtibe
Copy link
Contributor

schtibe commented Sep 17, 2015

@jsenecal Yes and no. Actually, with this commit it would've been underscored (which wouldn't be the correct behaviour as well I guess?) Commit 5b64567 then removed the default underscore behaviour without introducing the lowercase-ing again.
Pull Request #92 wouldn't fix that either, and just return the values as-is. But it's exactly the place to fix it, see my comments there.

@schtibe
Copy link
Contributor

schtibe commented Sep 17, 2015

@martinmaillard
Copy link
Contributor Author

That's great ! Thank you !

Now I'm gonna be really annoying: in my actual use case, the questions relationship is polymorphic. Each instance can have a different type subclassing Question (GreatQuestion, SuperQuestion). I think the whole point of specifying the type of each record in json-api is to support this kind of use case. So for ManyRelatedField and ListSerializer, the relation_type should probably be extracted for each instance, instead of once for the whole collection. What do you think ? Do you want me to make a PR so we can discuss on actual code ?

@jerel
Copy link
Member

jerel commented Sep 17, 2015

That sounds like a valid use case. I would welcome a PR so we can discuss it further

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants