4

I can get my file to save to disk where I tell it to, but can't get it to save to the instance and I haven't the slightest idea why!

models.py

    class Song(models.Model):
        name = models.CharField(max_length=50)
        audio_file = models.FileField(upload_to='uploaded/music/', blank=True)

views.py

    def create_song(request, band_id):
        if request.method == 'POST':
            band = Band.objects.get(id=band_id)
            form = SongForm(request.POST, request.FILES)
            if form.is_valid():
                handle_uploaded_file(request.FILES['audio_file'])
                form.save()
                return HttpResponseRedirect(band.get_absolute_url)
        else:
            form = SongForm(initial={'band': band_id})
        return render_to_response('shows/song_upload.html', {'form': form}, context_instance=RequestContext(request))

handle_uploaded_file

    def handle_uploaded_file(f):
        ext = os.path.splitext(f.name)[1]
        destination = open('media/uploaded/music/name%s' %(ext), 'wb+')
        for chunk in f.chunks():
            destination.write(chunk)
        destination.close()

song_upload.html (relevant part)

    {% block main %}
    {{band.name}}
        <form enctype="multipart/form-data" method="post" action="">{% csrf_token %}
           {{ form.as_p}}
           <input type="submit" value="Add song" />
        </form>
    {% endblock %}

forms.py

    class SongForm(forms.ModelForm):
        band = forms.ModelChoiceField(queryset=Band.objects.all(), widget=forms.HiddenInput) 
        def clean_audio_file(self):
            file = self.cleaned_data.get('audio_file',False)
            if file:
                if file._size > 10*1024*1024:
                    raise forms.ValidationError("Audio file too large ( > 10mb)")
                if not file.content_type in ["audio/mp3", "audio/mp4"]:
                    raise forms.ValidationError("Content type is not mp3/mp4")
                if not os.path.splitext(file.name)[1] in [".mp3", ".mp4"]:
                    raise forms.ValidationErorr("Doesn't have proper extension")
            else:
                raise forms.ValidationError("Couldn't read uploaded file")
        class Meta:
            model = Song

The file is right there in media/uploaded/music, but in admin audio_file is blank, and if i set blank=False (which is what I want to do) for audio_file, I'm told this field is required. What gives??

Thanks in advance! Been at this one for a while now, docs seem light to me (newb).

Community
  • 1
  • 1
Matt Parrilla
  • 3,171
  • 6
  • 35
  • 54

2 Answers2

1

clean_audio_file should return the cleaned data for this specific field, so you will need to add a return file to it!

From django's documentation:

Just like the general field clean() method, above, this method should return the cleaned data, regardless of whether it changed anything or not.

Bernhard Vallant
  • 49,468
  • 20
  • 120
  • 148
  • bah. that's what i get for not reading carefully... I know it must be very frustrating when you take the time to help someone with a problem, and they don't read your reply carefully. My sincere apologies. You were right 16 hours ago! – Matt Parrilla Jun 26 '11 at 18:17
1

As far as I know, you don't need to handle_uploaded_file(). You're using ModelForm and models.FileField. It supports upload_to argument, and saves the file to the destination automatically.

The basic file uploading example from Django docs is using Form and forms.FileField, and because of that it involves handle_uploaded_file() function.

So, try to simply remove this line:

handle_uploaded_file(request.FILES['audio_file'])

from your view and please tell what happens then.

EDIT: Also, @lazerscience is right, you need to add

return self.cleaned_data

at the end of your clean_audio_file(). (But not return file, since you always should return the whole cleaned data dictionary even if you're cleaning just one field.)

Anton Strogonoff
  • 32,294
  • 8
  • 53
  • 61
  • Made the two changes you mentioned and am now getting an error message saying: 'dict' object has no attribute '_committed'. The traceback includes only one line of code I wrote myself: `form.save()`. If I remove form.save() I don't see the error, but the form doesn't save either! As usual with file-upload, google is not particularly helpful and I don't know where to begin on docs... Thanks in advance! – Matt Parrilla Jun 26 '11 at 14:24
  • This is very strange. Well, seems like I was wrong, and @lazerscience was indeed right, and only file needs to be returned from the cleaning method. (Since `_committed` is supposed to be a part of filefield's state, but it's absent on `cleaned_data` dictionary.) Try to `return file` instead of `return self.cleaned_data`. I'm still sure, though, that `models.FileField` will handle file storage. – Anton Strogonoff Jun 26 '11 at 17:21
  • Thank you very much Anton. @lazerscience's solution, plus removing handle_uploaded_file() works perfectly! – Matt Parrilla Jun 26 '11 at 18:21