Search code examples
djangoangularfileuploadtypeerror

TypeError: MyModel() got unexpected keyword arguments: 'file'


I send a file and some data from my angular frontend to my django backend and encounter the error mentioned in the title. Bellow is my code:

Model:

class TaskData(models.Model):
    title = models.CharField(max_length=255)
    file = models.FileField(
        upload_to="uploads/", null=True, blank=True, name="upload_file"
    )

View:

file = request.FILES["file"]

task = TaskData(
    title=title,
    file=file,

)

task.save()

this is the exception:

TypeError: TaskData() got unexpected keyword arguments: 'file'

this is how I send the data from Angular (15):

const formData: FormData = new FormData()
if (this.selectedFile) formData.append('file', this.selectedFile)

formData.append('title', this.title)

return this.httpClient.post<ITask>(
      `${environment.baseUrl}/tasks`,
      formData
)

I don't get what I did wrong.


Solution

  • Well the answer is very stupid: in the model I specified the name as upload_file and therefore file throws a TypeError. This is very stupid especially because it cost me multiple hours... :(