如何在google appengine / django表单中添加自定义验证器

时间:2010-09-21 15:21:26

标签: django google-app-engine django-forms

我正在使用Django和Google App Engine Django Helper开发一个在Google App Engine上使用的应用。

某个型号看起来像这样:

from appengine_django.models import BaseModel
from google.appengine.ext import db
from google.appengine.ext.db.djangoforms import ModelForm

class Server(BaseModel):
    name = db.StringProperty(required=True)
    ip = db.StringProperty()
    status_ok = db.BooleanProperty(default=False)

    def __unicode__(self):
        return self.name

class ServerForm(ModelForm):
    class Meta:
        model = Server

如何为此代码添加自定义验证程序(例如字符串的最小和最大长度),以便form.is_valid()方法相应地执行操作?

1 个答案:

答案 0 :(得分:1)

ModelFormForm,然后你可以

class ServerForm(ModelForm):
    class Meta:
        model = Server

    def clean_name(self):
        """ Define a method to check the name field """

    def clean(self):
        """ Define a clean method for all the form """

没有问题