开发者

How can I add a custom validator to a google appengine / django form

I am developing an app for use on Google App Engine with Django and Google App Engine Django Helper.

A certain model is looking like this:

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

How can I add a custom validator (for example a minimum and maximum length for a string) to this code so that the form.is_valid() method will act accordingly?


the ModelForm is a Form then you can do

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 """

without problem

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜