validators as Meta attribute in ModelForms

3 views
Skip to first unread message

Steven Armstrong

unread,
Apr 13, 2008, 12:09:14 PM4/13/08
to django-d...@googlegroups.com
Hi

I've made a small change to the ModelForm framework to allow declarative
definition of validators. This allows for quick and easy reuse of
validators and means I have to write less clean_foo methods. This works
both in the admin as in custom views.


What used to be written like this:

class Domain(models.Model):
name = models.CharField(_('Name'), max_length=255, unique=True,
validator_list=[validators.isValidHostName])
active = models.BooleanField(_('Active'), default=True)


Could then be written like this:

class Domain(models.Model):
name = models.CharField(_('Name'), max_length=255, unique=True)
active = models.BooleanField(_('Active'), default=True)

class DomainModelForm(forms.ModelForm):
class Meta:
model = Domain
validators = {
'name': [validators.isValidHostName],
}


And can be used in the admin like this:

from foo.forms import DomainModelForm
from foo.models import Domain
class DomainAdmin(admin.ModelAdmin):
list_display = ('name', 'active')
list_filter = ('active',)
search_fields = ('name',)
form = DomainModelForm
admin.site.register(Domain, DomainAdmin)


Anybody interested in this?
Is it worth opening a ticket with a patch?

Cheers
Steven

Honza Král

unread,
Apr 15, 2008, 6:13:49 PM4/15/08
to django-d...@googlegroups.com
Hi,
check out ticket #6845 ( http://code.djangoproject.com/ticket/6845 ).
It enables you to define validators on both DB Fields and FormFields
and should make your work much easier.

--
Honza Král
E-Mail: Honza...@gmail.com
ICQ#: 107471613
Phone: +420 606 678585

Steven Armstrong

unread,
Apr 15, 2008, 8:23:33 PM4/15/08
to django-d...@googlegroups.com
Honza Král wrote on 04/16/08 00:13:

> Hi,
> check out ticket #6845 ( http://code.djangoproject.com/ticket/6845 ).
> It enables you to define validators on both DB Fields and FormFields
> and should make your work much easier.
>

Hi Honza

Found your ticket just after posting this :)
Will keep an eye on it.

Thanks for the pointer.


Reply all
Reply to author
Forward
0 new messages