Skip to content
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

inflection is not performed by AutoPrefetchMixin for included resources #871

Open
masda70 opened this issue Dec 12, 2020 · 1 comment
Open
Labels
bug good first issue Good for newcomers

Comments

@masda70
Copy link

masda70 commented Dec 12, 2020

On v4.0.0, inflection is not performed by the AutoPrefetchMixin for included resources, contrary to the JSONRenderer.

Suppose the following data model, serializers and viewsets:

from django.db import models
from rest_framework import viewsets, mixins
from rest_framework_json_api import views, serializers

class Album(models.Model):
    title = models.TextField()

class Track(models.Model)
    title = models.TextField()
    is_part_of = models.ManyToManyField(Album)

class AlbumSerializer(serializers.ModelSerializer):
    class Meta:
        model = Album
        fields = ["title"]

class TrackSerializer(serializers.ModelSerializer):
    class Meta:
        model = Track
        fields = ["title", "is_part_of"]

    included_serializers = {
        "is_part_of": AlbumSerializer,
    }
class Track(
    views.AutoPrefetchMixin,
    views.RelatedMixin,
    mixins.ListModelMixin,
    viewsets.GenericViewSet
):
    serializer_class = serializers.TrackSerializer
    queryset = Track.objects.all()

Both GET /tracks?include=is-part-of and GET /tracks?include=isPartOf correctly return the set all of tracks along with their albums' titles embedded within the included section of the response.

However, prefetch of the tracks' related album objects is not performed, as one would expect from views.AutoPrefetchMixin.

My workaround is to perform GET tracks?include=is_part_of, which returns the same thing as the above two queries but also performs prefetching.

My investigation has shown me that the AutoPrefetchMixin does not perform inflection of the result of get_included_resources that it uses to create the argument passed to prefetch_related. The JSONRenderer does the following instead:

        included_resources = copy.copy(included_resources)
        included_resources = [inflection.underscore(value) for value in included_resources]
@sliverc
Copy link
Member

sliverc commented Dec 12, 2020

Thanks for reporting this. When having a quick glance at the code it seems that this is indeed an issue.

Marking this as a bug. PR is very welcome.

For some who wants to have a look at this, here is the best spot to start.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants