Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i use validation in admin.py while overriding save_model() function?

Admin.py

class CourseAdmin(admin.ModelAdmin):

list_display = ('course_code', 'title', 'short' )

 def save_model(self, request, obj, form, change):
    import os

    #obj.author = request.user
    dir_name = obj.course_code
    path = settings.MEDIA_ROOT +os.sep+'xml'+os.sep+dir_name

    #if user updates course name then course would be renames
    if change:
        dir_name = Course.objects.get(pk=obj.pk).course_code
        src = settings.MEDIA_ROOT +os.sep+'xml'+os.sep+dir_name      
        os.rename(src,path)
    else:
        if not os.path.exists(path):
            os.makedirs(path)
            obj.save()

        else:
            raise ValidationError('Bla Bla')


admin.site.register(Course, CourseAdmin)

when i raise validation Error it doesn't work and shows error page with Exception Type: Validation Error Exception Value:[u'Bla Bla']

like image 732
Anshul Avatar asked Feb 25 '11 06:02

Anshul


1 Answers

Here's an example:

def clean_name(self):
    if something:
        raise forms.ValidationError("Something went wrong")
    return self.cleaned_data["name"]
like image 152
Asdf Avatar answered Oct 26 '22 19:10

Asdf