When I run the save override method, the mp3 file is saved to the specified folder, only it keeps saving itself to that dir over and over again until I eventually restart the server.
The file is saved to the right place and it can be played with VLC, so at least there's that...
It would appear that the problem persists with both of the save models I've included. I'm guessing that the super().save() is never run, but I can't really tell what's going on, simply put.
What am I doing wrong, why is the multi-saving happening and how should I fix it?
from django.db import models
from PIL import Image
from gtts import gTTS
from io import BytesIO
import tempfile
from django.core.files import File
class VoiceModel(models.Model):
name = models.CharField(max_length=50)
...
audiofile = models.FileField(upload_to='sounds/loads', max_length=100, blank=True, null=True) # editable=False)
def __str__(self):
return self.name
def save(self, *args, **kwargs):
new_string = 'repeat after me: ' + str(self.name)
file_name = '{}.mp3'.format(str(self.name).lower().replace(' ', '_'))
make_sound = gTTS(text=new_string, lang='en')
mp3_fp = BytesIO()
make_sound.write_to_fp(mp3_fp)
self.audiofile.save(file_name, mp3_fp)
super(VoiceModel, self).save(*args, **kwargs)
# def save(self, *args, **kwargs):
# new_string = 'repeat after me: ' + str(self.name)
# audiofile = gTTS(text=new_string, lang='en')
# with tempfile.TemporaryFile(mode='wb+') as f:
# # with tempfile.TemporaryFile(mode='w') as f:
# # with tempfile.TemporaryFile(mode='rb+') as f:
# audiofile.write_to_fp(f)
# file_name = '{}.mp3'.format(self.name).lower().replace(' ', '_')
# self.audiofile.save(file_name, File(file=f))
# super(VoiceModel, self).save(*args, **kwargs)
Adding a save=False
in the save method did the trick.
self.audiofile.save(file_name, mp3_fp, save=False)