Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

how to show relative URLs instead full url in payload #923

Unanswered
sanjay-raut asked this question in Q&A
Discussion options

I am having trouble showing relative links (self, related).
in payload it's always giving me a full URL like http://localhost:8000/modules/3 , what I want is /modules/3 instead showing full URL.
how can I achieve it?

You must be logged in to vote

Replies: 4 comments

Comment options

In exceptional cases relative urls might make sense but absolute urls are recommend by DRF see here

This said you can overwrite get_url in your view (maybe create your own project base view) and pass on request as None so DRF returns relative urls.

This could look like this (just from the top ofof my head without any testing):

def get_url(self, name, view_name, kwargs, request):
 return super().get_url(name, view_name, kwargs, None)

Does this work?

You must be logged in to vote
0 replies
Comment options

@sliverc no, I added this in my view but still the same.

we need relative URLs in payload because our APIs are behind API-gateway.

You must be logged in to vote
0 replies
Comment options

@sliverc get_url is not part of a view, it's a part of ResourceRelatedField, so I had to inherit this and override the get_url method.
this way I am able to get relative URLs for related links.

You must be logged in to vote
0 replies
Comment options

I was thinking you are using relationship view where get_url would need to be overwritten as well.

But of course you also need to do that for ResourceRelatedField. Also make sure that your serializers set serializer_related_field to the new resource related field.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #902 on April 18, 2021 17:50.

AltStyle によって変換されたページ (->オリジナル) /