Skip to content

Support specifying lookup name for hyperlinked fields to related_views #919

Closed
@SafaAlfulaij

Description

@SafaAlfulaij

Refer to: #904

There is no way currently to pass a custom lookup name of related_views to hyperlinked fields.
The below field should link to /entries/123/comments instead of /entries/123/comments_hyperlinked.

    comments_hyperlinked = relations.HyperlinkedRelatedField(
        related_link_view_name="entry-related",
        related_link_url_kwarg="entry_pk",
        self_link_view_name="entry-relationships",
        many=True,
        read_only=True,
        source="comments",
    )

Activity

sliverc

sliverc commented on Apr 20, 2021

@sliverc
Member

This is in the example app like this but I wonder whether it makes any sense at all to define a related field to the same relationship twice on the same serializer: one hyperlinked and one as resource.

I haven't tested this but if the hyperlink is defined like the below it should actually work right?

    comments = relations.HyperlinkedRelatedField(
        related_link_view_name="entry-related",
        related_link_url_kwarg="entry_pk",
        self_link_view_name="entry-relationships",
        many=True,
        read_only=True,
    )

If so my question would be what the use case is for referencing the same relation once hyperlinked and once a resource is?

sliverc

sliverc commented on Sep 23, 2021

@sliverc
Member

I thought about this issue again and actually the url the hyperlinked related field produces is correct, the issue is though that the test EntrySerializer does not either add a included_serializers or related_serializers for the field comments_hyperlinked. So this is a issue in the configuration of the example app. As of #468 we are currently cleaning up all the tests and then the example app so this will be addressed there too. Therefore closing this issue.

If I missed anything and this is actually a DJA and not a test issue please feel free to comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sliverc@SafaAlfulaij

        Issue actions

          Support specifying lookup name for hyperlinked fields to related_views · Issue #919 · django-json-api/django-rest-framework-json-api