Django admin field validation to return error not raise exception in application

9.2k views Asked by At

I have a Django model where it has an upload field for images, am trying to validate the image field to check image size before storing and return elegant error message for the user to correct.

I tried to use the following code but it did not work

admin.py

class BonanzaAdmin(TranslatableAdmin):
    list_display = ['get_bonanza_name', 'user_profile', 'publish_date', 'created_by', 'created_at', 'all_translations']

    def clean_image(self):

        image = self.cleaned_data.get('image')
        if not image:
            raise forms.ValidationError("No image!")
        else:
            w, h = get_image_dimensions(image)
            if w != 1170:
                raise forms.ValidationError("The image is %i pixel wide. It's supposed to be 1170px" % w)
            if h != 500:
                raise forms.ValidationError("The image is %i pixel high. It's supposed to be 500px" % h)
        return image 


    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_by = request.user
        obj.save()

The whole clean_image() is not being called, i also tried to reverse the name to image_clean() but also didn't work. Any ways, i tried to move the validation to the save_model

class BonanzaAdmin(TranslatableAdmin):
    list_display = ['get_bonanza_name', 'user_profile', 'publish_date', 'created_by', 'created_at', 'all_translations']


    def save_model(self, request, obj, form, change):
        image = obj.image
        if not image:
            raise forms.ValidationError("No image!")
        else:
            w, h = get_image_dimensions(image)
            if w != 1170:
                raise forms.ValidationError("The image is %i pixel wide. It's supposed to be 1170px" % w)
            if h != 500:
                raise forms.ValidationError("The image is %i pixel high. It's supposed to be 500px" % h)
        if not change:
            obj.created_by = request.user
        obj.save()

now the second piece of code works fine, it goes through the validation process but it does not show a simple error message. Instead, it throws an exception! how can i make it show an error so the user would correct and try again?

P.S am using django 1.4 and django-hvad for translation that's why am inheriting from TranslatableAdmin

Regards

2

There are 2 answers

1
Aleksej Vasinov On BEST ANSWER

As demalexx said, you should use additional form to validate your model.

class Bonanza(models.Model):
    user_profile = models.CharField(max_length = 128)
    publish_date = models.DateField()

Use any validation in your admin form:

class BonanzaAdminForm(forms.ModelForm):
    class Meta:
        model = Bonanza
    def clean(self):
        # Validation goes here :)
        raise forms.ValidationError("TEST EXCEPTION!")

And simple auto-generated admin:

class BonanzaAdmin(admin.ModelAdmin):
    form = BonanzaAdminForm

admin.site.register(Bonanza, BonanzaAdmin)
0
demalexx On

You inherit ModelAdmin and it doesn't have validation methods. Instead you should override form attribute to provide custom form and add validation method to that form. There is docs about custom validation in admin: https://docs.djangoproject.com/en/1.4/ref/contrib/admin/#adding-custom-validation-to-the-admin