Hi David. Thanks for the response. Took a detour and just returned to this task.
I decided to do a quick check on file extension after all. This is in the admin area and for now this will have to do: def save_model(self, request, obj, form, change): try: file_name = request.FILES['audio_file'].name if file_name.split('.')[1] == 'mp3': obj.save() else: raise forms.ValidationError("The podcast file must be of type .mp3") except: raise forms.ValidationError("A podcast file is required.") Just out of curiosity, what does your custom FielField look like? Many thanks! On Fri, Mar 25, 2011 at 5:20 PM, David De La Harpe Golden < david.delaharpe.gol...@ichec.ie> wrote: > On 25/03/11 13:48, Sithembewena Lloyd Dube wrote: > > Hi folks, > > > > I have a model with a FileField, and I need to chech that the file is > .mp3 > > before it is uploaded. How would I go about this without the hack of > getting > > the file name and slicing it? > > > > Uh. File names are just names. There's no guarantee a file called > blah.mp3 is an mp3. I assume what you actually want is to try to make > sure the client only uploads mp3 audio files. > > Browsers do typically try to respect the "accept" html attribute [1] in > a file upload field in a form to limit the options in the file select > dialog to files of the specified type (at least by default), so could > say accept="audio/mpeg" (strictly that'll cover mp1 and mp2 too, but meh > [2]) - but try adding it as an attr on the widget of the field of the > django form.[3] > > However, remember, /you can't trust the client/. So you're going to > have to let some or all of the upload happen and check or recheck on the > server, at least if you really care if it's really an mp3 file. > > We use a FileField subclass that just call python-magic [4] for a quick > initial mimetype check (and pyclamd [5] to virus scan), though of course > it's not infallible either, you may (will) need further file format > specific validation if you care whether the file is valid and > uncorrupted (and probably ultimately a human approval process in your > case, automatically judging if a completely valid .mp3 is just 4 minutes > of some guy screaming obscenities is difficult...) > > [1] http://www.w3.org/TR/html401/interact/forms.html#adef-accept > [2] http://tools.ietf.org/html/rfc3003 > [3] > > http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Widget.attrs > [4] http://packages.debian.org/sid/python-magic > [5] http://xael.org/norman/python/pyclamd/ > > > > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- Regards, Sithembewena Lloyd Dube -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.