Trouble raising forms.ValidationError in save() override in admin.py

4,308 views
Skip to first unread message

Sithembewena Lloyd Dube

unread,
Mar 30, 2011, 10:32:38 AM3/30/11
to django...@googlegroups.com
Hi all,

In the admin.py code of my project, I have an override to a save() method where I check a file upload's extension and, based on that,
should either save the instance object or update show an error message on the admin form as follows:

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.")

The issue is that when a forms.ValidationError is raised as expected, I am getting a stack trace instead of the error message being displayed on the form.
I do have debug set to True in my settings as this is a development environment, but how can I cause the errors to be displayed on the form instead of a stack trace as follows?:

ValidationError at /admin/browse/podcast/add/

[u'A podcast file is required.']
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/browse/podcast/add/
Django Version: 1.2.3
Exception Type: ValidationError
Exception Value:
[u'A podcast file is required.']
Exception Location: C:\Websites\TPFRepository\thepokerfarm\..\thepokerfarm\browse\admin.py in save_model, line 212
Python Executable: C:\Python26\python.exe
Python Version: 2.6.5

...etc

Thanks!
--
Regards,
Sithembewena Lloyd Dube

Shawn Milochik

unread,
Mar 30, 2011, 10:37:24 AM3/30/11
to django...@googlegroups.com
No, the forms.ValidationError has to be raised in one of the clean() methonds of your model. Otherwise it will result in a stack trace.


Sithembewena Lloyd Dube

unread,
Mar 31, 2011, 6:09:17 AM3/31/11
to django...@googlegroups.com, Shawn Milochik
Hi Shawn,

Thanks for the feedback. I read the docs and defined a clean_audio_file method in my ModelAdmin class. I dropped the Podcast table, made the field not mandatory in the model and did a syncdb (to test my clean method).

Still a podcast can be saved without the audio_file: the code in the clean method does not appear to run. Please see the code below:

class PodcastAdmin(admin.ModelAdmin):
     list_display = ('title', 'audio_file', 'active', 'user')

     def clean_audio_file(self, request, obj, form, change):
          data = self.cleaned_data['audio_file']
          if not data or len(data) < 1 or data == "":
               raise forms.ValidationError("Provide a file!")
          return data

admin.site.register(Podcast, PodcastAdmin)

Am at a loss.

On Wed, Mar 30, 2011 at 4:37 PM, Shawn Milochik <sh...@milochik.com> wrote:
No, the forms.ValidationError has to be raised in one of the clean() methonds of your model. Otherwise it will result in a stack trace.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Daniel Roseman

unread,
Mar 31, 2011, 7:10:40 AM3/31/11
to django...@googlegroups.com, Shawn Milochik
On Thursday, March 31, 2011 11:09:17 AM UTC+1, Lloyd Dube wrote:
Hi Shawn,

Thanks for the feedback. I read the docs and defined a clean_audio_file method in my ModelAdmin class.

That's not what the docs say. They say you should create it on the Form. And where did you get the idea that the clean method takes any of those parameters?
--
DR.

Gonzalo Amadio

unread,
May 3, 2018, 10:37:08 AM5/3/18
to Django users
This is very old, but in the case someone red this.

If you do it on the save method of the admin, you MUST save the model.

So you should override admin's form. 

Reply all
Reply to author
Forward
0 new messages