Manish Shah
Manish Shah

Reputation: 401

How to remove `LOCATION` from the header options in DRF?

I have a serializer which contains a URL field, By default, if there is a field named as URL then the value of this field is added in the HEADER option as LOCATION, I don't want to do this, and would like to remove the LOCATION option from the header.

This is my serializer:-

class DemoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Demo
        fields = ('type', 'protocol', ---- 'url', 'somefield')

Now when the above serializer is used with POST/PUT/PATCH request to send data, I get this as response:- enter image description here

Is there any method, or way I can remove the LOCATION option from the header, without effecting my other serializers and view in the project??

Upvotes: 2

Views: 609

Answers (2)

Hans Bambel
Hans Bambel

Reputation: 956

You could change or set the URL_FIELD_NAME to None which is used for setting the Location header:

REST_FRAMEWORK = {
    "URL_FIELD_NAME": None,  # This field is used in the Location header on a created resource
}

Upvotes: 0

Utkucan Bıyıklı
Utkucan Bıyıklı

Reputation: 1117

In View which is you want to remove Location header, you should override get_success_headers function.

Default

    def get_success_headers(self, data):
        try:
            return {'Location': str(data[api_settings.URL_FIELD_NAME])}
        except (TypeError, KeyError):
            return {}

Override

    def get_success_headers(self, data):
        return {}

Upvotes: 1

Related Questions