Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'file' object has no attribute '_committed'

I have a DjangoFileField in my model. I am trying to convert the type of the audio from that FielField to mp3 and then again trying to save it. But after converting the type and exporting it using pydub it is returning the following error

AttributeError: 'file' object has no attribute '_committed'

My code is like this

def get_from_function(AudioSegment, format):

    form = "from_{0}".format(format)
    print form
    if hasattr(AudioSegment, form):
        return getattr(AudioSegment, form)
    return None


    audio = request.FILES.get('audio', None)
    if audio:
        name_list = audio.name.rsplit(".")
        voice_format =name_list[1]
        from_format = get_from_function(AudioSegment, voice_format)
        if from_format and callable(from_format):
            sound = from_format(audio)
            audio = sound.export("media/{0}".format(name_list[0]), mp3")

when i print the audio it prints

<open file 'media/barsandtone', mode 'wb+' at 0x7f771e5e2f60>

and when i print the type of file it prints

<type 'file'>

but when i assign the audio field to django model like

Mymodel.objects.create(audio=audio)

it gives error

AttributeError at /create/
'file' object has no attribute '_committed'

What is the correct way to save the exported file into django model

like image 719
Abdul Rauf Avatar asked Apr 13 '16 21:04

Abdul Rauf


2 Answers

django needs a ContentFile usually to do this passing it a stream of data, and it doesn't work the way you usually pass arguments to a model. The proper way to do this is the following

from django.core.files.base import ContentFile
[...]
mymodel = Mymodel()
mymodel.audio.save(audio.name, ContentFile(audio.read()))

don't forget to pass the stream to ContentFile. Django won't raise any errors if you pass it ContentFile(audio) but in that case you won't save the file content..

like image 129
John Paraskevopoulos Avatar answered Oct 16 '22 08:10

John Paraskevopoulos


I got same error that is solved now i would suggest you to open that file in read mode then save it. here is my code :

from django.core.files import File as DjangoFile
file_obj1 = DjangoFile(open(file_name, mode='rb'), name=file_name)
File.objects.create(title=file_name, file=file_obj1, content_object=task_obj, author=task_obj.client)
like image 23
Ankit Kumar Avatar answered Oct 16 '22 07:10

Ankit Kumar