2010-09-21 73 views
2

我正在開發一個應用程序,以便在帶有Django的Google App Engine上使用,並且可以使用Google App Engine Django Helper如何將自定義驗證器添加到Google Appengine/Django表單

一定模型看起來像這樣:

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 

如何I(一個最小和一個最大串長度例如)添加自定義驗證該代碼使得form.is_valid()方法會採取相應措施?

回答

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

沒有問題

相關問題