-
Notifications
You must be signed in to change notification settings - Fork 299
how to show relative URLs instead full url in payload #923
-
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?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 4 comments
-
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?
Beta Was this translation helpful? Give feedback.
All reactions
-
@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.
Beta Was this translation helpful? Give feedback.
All reactions
-
@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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.