Validation within models.py

1 view
Skip to first unread message

Bala kumar

unread,
Jul 1, 2009, 7:16:18 AM7/1/09
to Django developers
Hi,

Is it possible to apply validation on particular field within
models.py?.

I have the below model defined, the form is dynamically managed by
Django Admin

class CategoryMapping(models.Model):
categoryid = models.AutoField(primary_key=True,)
categorytype = models.CharField( max_length = 250, unique =
True,)
displayname = models.CharField( max_length = 250, blank =
False,)
description = models.TextField( blank = True,)
def __str__(self):
return self.categorytype
class Meta:
db_table = 'category_mapping'


Here I want to apply a validation for the field 'categorytype' in
models.py itself without calling forms.py

eg:

def clean_categorytype(self):
if self.cleaned_data.get('categorytype', 'test'):
raise forms.ValidationError(u'Please enter valid category
type')
else:
return self.cleaned_data.get('categorytype')

is it possible?

Honza Král

unread,
Jul 2, 2009, 11:19:58 AM7/2/09
to django-d...@googlegroups.com
Hello,

this is made possible by #6845 which is a Google Summer of Code
project for this year. You can check the progress at
http://code.djangoproject.com/browser/django/branches/soc2009/model-validation

Honza Král
E-Mail: Honza...@gmail.com
Phone: +420 606 678585
Reply all
Reply to author
Forward
0 new messages