Dries
Dries

Reputation: 21

Model validation in Django 1.1

I'm using Django 1.1 I would like to perform validation on a model; specifically, check the extension of a file in a FileField. I can do the extension check fine, but I don't know how to show an error in the admin panel if it's the wrong extension; similar to when you forget to fill in a required field.

I've tried 2 ways to do it.

Is there any way I can do what I want in Django 1.1?

Upvotes: 2

Views: 188

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599630

Django 1.1 doesn't have model validation. The only other place to do validation is on the form - all you have to do is define a custom modelform with your clean method and then tell the admin to use it.

class MyModelForm(forms.ModelForm):
    def clean_myfilefield(self):
        ... do validation or raise forms.ValidationError('message')

class MyModelAdmin(admin.ModelAdmin):
    model = MyModel
    form = MyForm

Note that just checking the extension is not enough to be sure you're getting the file type you expect.

Upvotes: 3

Related Questions