django-rest-polymorphic icon indicating copy to clipboard operation
django-rest-polymorphic copied to clipboard

Disable resourcetype?

Open ssandr1kka opened this issue 3 years ago • 1 comments

Hi guys,

Very good work, I just have one query regarding this. Basically, when I get (Response.data).keys() it is really important for me to receive only attributes defined in within the model. However, I do also get resourcetype. Is there a way to disable that? I do not want to be receiving resourcetype as a parameter at all. I tried all that was in the readme, but nothing worked so far.

Any help?

ssandr1kka avatar Apr 01 '21 13:04 ssandr1kka

You can do it by changing the to_representation() function:

def to_representation(self, instance):
        if isinstance(instance, Mapping):
            resource_type = self._get_resource_type_from_mapping(instance)
            serializer = self._get_serializer_from_resource_type(resource_type)
        else:
            resource_type = self.to_resource_type(instance)
            serializer = self._get_serializer_from_model_or_instance(instance)

        ret = serializer.to_representation(instance)
        # ret[self.resource_type_field_name] = resource_type   //Comment out this line 
        return ret
        

I didn't test it out, but it should be ok.

osousa avatar May 04 '21 16:05 osousa