在下面的示例代码中,我说明了如何解决将请求从控制器传递到模型的问题。有一个更好的方法吗?
模型/MyModel.py
from google.appengine.ext import db
class MyModel(db.model):
a = db.StringProperty(required=True)
b = db.StringProperty(required=True)
c = db.StringProperty(required=True)
class Update:
def __init__(self, x)
self.x = x
def do_something(self)
myquery = db.GqlQuery('SELECT * FROM MyModel WHERE a = :1' self.x)
v = myquery.get()
if not v:
somevalue = MyModel(a = self.request.get('a'), # Doesn't work unless
b = self.request.get('b'), # the request has been
c = self.request.get('c')) # passed
somevalue.put()
控制器/MyController.py
import webapp2
from models.MyModel import Update
class WebHandler(webapp2.RequestHandler):
def get(self):
var_x = "string"
var_y = "string"
z = Update(var_x)
z.request = self.request # <--- Is there a better way?
z.do_something()
编辑:(5/30/2012)我最终创建了一个函数来将请求变量作为参数传递给模型。我希望有一些蟒蛇魔法可以避免所有重复,但我没有找到。