Custom Validation in Django 1.0.2 Final

4 views
Skip to first unread message

Harish

unread,
Apr 7, 2009, 2:58:48 AM4/7/09
to Django users
Hi Folks,

I am wrote a simple application in djang0 1.0.2 to check the custom
validation. I will past the model.py and admin.py code here.

model.py
-----------
from django.db import models
from django.contrib import admin
from django import forms

class Personal(models.Model):
firstName = models.CharField(
max_length = 20,
blank = True,
null = True,
)

age = models.IntegerField(
blank = True,
null = True,
)

salary = models.DecimalField(
max_digits = 7,
decimal_places = 2,

admin.py
-----------
from MyApp.Personal.models import Personal
from django.contrib import admin
from django import forms
from django.forms import ValidationError

class DocumentValidationError(forms.ValidationError):
def __init__(self):
super(DocumentValidationError, self).__init__(_(u'Document
types accepted: ') + ', '.jo\
in(salary.valid_file_extensions))

class PersonalForms(forms.ModelForm):
model=Personal
default_err_msg = {'invalid':u'Salary cannot be negetive',}
def clean(self):
super(PersonalForms,self).clean()
salary = self.cleaned_data['salary']
if salary is not None and float(salary) < 0:
raise forms.ValidationError(self.default_err_msg
['invalid'])
return self.cleaned_data

class PersonalAdmin(admin.ModelAdmin):
form = PersonalForms
search_fields = ['email']



If salary is found negative it displays the error message in beginning
of the form(that is after the message 'Please correct the error below.
'). Actually it should display the error message in the 'Salary'
section

I think, I am missing something here.
Can anyone help?




Thanks and Best Regards
Harish Bhat M

Malcolm Tredinnick

unread,
Apr 7, 2009, 3:05:12 AM4/7/09
to django...@googlegroups.com
On Mon, 2009-04-06 at 23:58 -0700, Harish wrote:
[...]

> If salary is found negative it displays the error message in beginning
> of the form(that is after the message 'Please correct the error below.
> '). Actually it should display the error message in the 'Salary'
> section
>
> I think, I am missing something here.

Since your field validation appears to only depend on the "salary" form
field, the easiest solution here is to write a clean_salary() method,
rather than using clean(). Then, any ValidationError raised will
automatically be assigned to the salary field.

Have a look at
http://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-a-specific-field-attribute and the surrounding documentation for some more information about this. In general, the clean() method is only useful if you're validating multiple fields simultaneously to check something. When the result of a particular check only depends on a single field, best to validate as part of that field's cleaning routine.

Regards,
Malcolm


Harish

unread,
Apr 7, 2009, 3:14:12 AM4/7/09
to Django users
Hi, Malcolm...


Thanks a lot... It is working Now..


On Apr 7, 12:05 pm, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:
> On Mon, 2009-04-06 at 23:58 -0700, Harish wrote:
>
> [...]
>
> > If salary is found negative it displays the error message in beginning
> > of the form(that is after the message 'Please correct the error below.
> > '). Actually it should display the error message in the 'Salary'
> > section
>
> > I think, I am missing something here.
>
> Since your field validation appears to only depend on the "salary" form
> field, the easiest solution here is to write a clean_salary() method,
> rather than using clean(). Then, any ValidationError raised will
> automatically be assigned to the salary field.
>
> Have a look athttp://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-a...and the surrounding documentation for some more information about this. In general, the clean() method is only useful if you're validating multiple fields simultaneously to check something. When the result of a particular check only depends on a single field, best to validate as part of that field's cleaning routine.
>
> Regards,
> Malcolm
Reply all
Reply to author
Forward
0 new messages