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.
{
"file_name": "test",
"file_type": "jpg",
"file": basex64 file,
"url_img": null
}
And i got this model
class imageModel(models.Model): id = models.AutoField(primary_key=True) url_img = models.textField(null=False)
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.
class ImageModelSerializer(serializers.ModelSerializer):
file_name = serializers.CharField(write_only=True)
file_type = serializers.CharField(write_only=True)
file = serializers.CharField(write_only=True)
class Meta:
model = ImageModel
fields = "__all__"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.