I'm using Django rest framework to create my api I have a model which receive an ImageField, if I tested the url with postman and attaching the image with the form-data works perfectly but when using angular as the frontend to send the image I get the following error:
Image: Array ['Image is not a file: check the method of codification of the formulary']
Here is my onSubmit method
add(endpoint:string, data:any) {
let url = `${this.baseUrl}/${endpoint}/`
let datos = JSON.stringify(data)
let token = new HttpHeaders().set('Content-Type', 'application/json').set('Authorization', `Token ${localStorage.getItem('token')}`)
let options = {headers:token}
return this.http.post(url, datos, options).pipe(catchError(this.handleError<any>()))
}
After debugging I noticed that the image field was empty so it's not sending any information but this happens just when I use the JSON.stringify(), is there anyway I could handle this?
I have also tried to use the FormData method but this then gave me a JSON Parse Error.
Model.py:
class Pelicula(models.Model):
titulo = models.CharField(max_length=100)
descripcion = models.TextField()
imagen = models.ImageField(upload_to='peliculas', null=True, blank=True)
reparto = models.TextField()
director = models.CharField(max_length=100)
duracion = models.IntegerField()
genero = models.CharField(max_length=100)
Serializer:
class PeliculaSerializer(serializers.ModelSerializer):
class Meta:
model = Pelicula
fields = ('titulo', 'imagen', 'descripcion', 'reparto', 'genero', 'duracion', 'director')