In the following example code I illustrate how I solved passing the request from the controller to the model. Is there a better way to do this?
models/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()
controllers/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()
Edit: (5/30/2012)
I ended up creating a function to pass the request variables to the Model as arguments. I was hoping there was some python magic that would avoid all the repetition but I didn’t find one.
handing over the request to the model means you’re mixing model and controller.
normaly the model shouldn’t even be aware that ther even is a request. geting the parameters from the request is the controller’s job, not the models. if your
do_somethingmethod has to work with parameters, then it should get them as arguments and return a result (usually an entity).