[Answer]-How to save a local file in a FileField in django

1πŸ‘

βœ…

It looks like your issue is not with the FileField, but occurs when saving the VoiceMessage instance instead.

In the traceback, the failure occurs at the end of FieldFile.save():

  File "/home/israelord/.virtualenvs/ringtu-env/local/lib/python2.7/site-packages/django/db/models/fields/files.py", line 95, in save
    self.instance.save()

Which means that everything went fine there, and only when this method in turns call save() on the vm object does the problem arise:

# FileField.save() calls instance.save() just before returning
# Here, self.instance is the vm object
def save(self, name, content, save=True): 
    name = self.field.generate_filename(self.instance, name) 
    self.name = self.storage.save(name, content) 
    setattr(self.instance, self.field.name, self.name) 

    # Update the filesize cache 
    self._size = content.size 
    self._committed = True 

    # Save the object because it has changed, unless save is False 
    if save: 
        self.instance.save() 

My best guess is the problem is on vm.date or another DateTimeField field, as the exception is raised in DateTimeField.to_python function. Can you check the type of msg['when']? You can also confirm this by skipping the instance save step:

vm.message.save(filename, File(msg['file']), False) # Added False
msg['file'].close()
vm.save() # Error should now be raised here

Leave a comment