我正在使用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()方法相应地执行操作?
答案 0 :(得分:1)
ModelForm
是Form
,然后你可以
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 """
没有问题