views:

253

answers:

1

Hi Guys,

I am quite new to Django, I'm having few problems with validation forms in Admin module, more specifically with raising exceptions in the ModelForm. I can validate and manipulate data in clean methods but cannot seem to raise any errors. Whenever I include any raise statement I get this error "'NoneType' object has no attribute 'ValidationError'". When I remove the raise part everything works fine.

Then if I reimport django.forms (inside clean method) with a different alias (e.g. from django import forms as blahbalh) then I'm able to raise messages using blahblah.ValidateException.

Any tips or suggestions on doing such a thing properly ?

Here's an example of what I'm doing in Admin.py:

admin.py

from django import forms from proj.models import * from django.contrib import admin

class FontAdminForm(forms.ModelForm):

class Meta:

    model = Font

def clean_name(self):

    return self.cleaned_data["name"].upper()

def clean_description(self):

    desc = self.cleaned_data['description']
    if desc and if len(desc) < 10:
        raise forms.ValidationError('Description is too short.')
    return desc

class FontAdmin(admin.ModelAdmin):

form = FontAdminForm
list_display = ['name', 'description']

admin.site.register(Font, FontAdmin)

-- Thanks, A

+3  A: 

You problem might be in the * import.

from proj.models import *

if proj.models contains any variable named forms (including some module import like "from django import forms), it could trounce your initial import of:

from django import forms

I would explicitly import from proj.models, e.g.

from proj.models import Font

If that doesn't work, see if there are any other variables name "forms" that could be messing with your scope.

You can use introspection to see what "forms" is. Inside your clean_description method:

print forms.__package__

My guess is it is not going to be "django" (or will return an error, indicating that it is definitely not django.forms).

John Paulett
yes you are correct, it wasn't using the django.forms at all therefore the app was breaking, it's fixed now. SO THANKS for explaining.
Nakh