我正在编写的应用程序的一部分大致结构如下:
class PostModel(db.Model):
some_property = db.WhateverProperty()
some_other_property = db.WhateverProperty()
class PostHandler(webapp.RequestHandler):
def get(self):
#code to generate form
def post(self):
#code to validate input from form
#create entity and put() it to datastore if input passes the validation
根据我所了解的MVC,这个验证逻辑应该在模型中,对吗?那么,我应该像这样做呢?
class PostModel(db.Model):
some_property = db.WhateverProperty()
some_other_property = db.WhateverProperty()
@staticmethod
def validation_logic(form_input):
#throw exceptions if validation fails
@staticmethod
def save_to_datastore(form_input):
#this would assume data already passed validation
#create entity and save it
class PostHandler(webapp.RequestHandler):
def get(self):
#code to generate form
def post(self):
try:
PostModel.validation_logic(form_input)
except CustomException,e:
self.redirect('/errorpage?msg='+e.msg)
PostModel.save_to_datastore(form_input)
这是一个好的MVC表单吗?