Skip to content
Advertisement

DRF add non model fields just to update or create model instance

I have this issue at the moment with DRF.

I’m recieving extra fields that the model is not using. But those values will define the fields within the model.

JavaScript

And i got this model

JavaScript

All I need is to parse file_name and file_type to upload img to create a url_img and upload it on cloud. Is there a way to do this via DRF?

Advertisement

Answer

The DRF way of doing this is, explicitly define your extra fields in the serializer class.

JavaScript

Here, I have used serializers.CharField–(DRF Doc) since all of those data are strings (if not, choose serializer fields accordingly).

Also, setting write_only=True–(DRF Doc) will help us to omit those fields from the response.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement