1st: "args" in grids is only the arguments the grid should not consider.
therefore if you use request.args(0) to identify the id of the record, 
grid(args=request.args(0)) is what you need to call.

2nd: prefetch the default author and set it as a default field for the 
table before returning the grid (something like db.dog.owner.default = 
whatever), just like you do in 5. The default will be set for the 
controller only and only if new is some request.args (BTW: better to check 
the existence in the predifined arg, that is *if request.args(2) == 'new'*is 
better (safer) than
* if 'new' in request.args*). Don't worry, it wont propagate in other 
places.




On Tuesday, September 11, 2012 10:55:31 PM UTC+2, Vincent wrote:
>
> I'm using SQLFORM.grid a lot and it is extremely valuable.
>
> I am in a position where I want to use request.args with a grid and I am 
> unsure what the right approach is.
>
> Let's say my db.py model is:
> db.define_table('person',
>                 Field('firstname'),
>                 Field('lastname'), format='%(firstname)s %(lastname)s')
> db.define_table('dog',
>                 Field('name'),
>                 Field('owner',db.person))
>
>
> and in my default controller contains:
> @auth.requires_login()
> def add_dog():
>     person = request.args[0]
>     grid = SQLFORM.grid(db.dog.owner == person, args=request.args)
>     return {'grid':grid}
>
> The objective here is to show a grid of dogs that belong to only a given 
> person ("default/add_dog/1" would show a grid of dogs belonging to 
> person.id=1) and this works fine. My regarding using the "Add" button.
> My desired behavior is as follows: when one clicks the "Add" button the 
> form is prepopulated with the correct owner. I am not sure what is the best 
> approach.
>
> The options I have been able to think of are:
> 1. Let the user define whatever owner they want and then update the owner 
> using a callback with the 'oncreate' grid argument.
> def add_dog():
>     person = request.args[0]
>     grid = SQLFORM.grid(db.dog.owner == person, args=request.args,oncreate
> =add_callback)
>     return {'grid':grid}
>
> def add_callback(form):
>     db.dog[form.vars.id] = {'owner':int(request.args[0])}
> However this is bad UI design as it makes the user think they can define 
> something that they can't
>
> 2. I can remove the "owner" field from the create form using createargs 
> and then define it with the callback, which is a little better:
> def add_dog():
>     person = request.args[0]
>     grid = SQLFORM.grid(db.dog.owner == person, args=request.args,oncreate
> =add_callback, createargs={'fields':['name']})
>     return {'grid':grid}
>
> def add_callback(form):
>     db.dog[form.vars.id] = {'owner':int(request.args[0])}
>
> 3. I can completely override the "Add" behavior by looking for "new" in 
> the args and defining my own function. But I would like to use the defaults 
> if possible.
>
> 4. I can mess around with the html in the view but I don't want to do that.
>
> 5. Finally the closest I get to what I want to do is this:
> def add_dog():
>     person = request.args[0]
>     if 'new' in request.args:
>         db.dog.owner.default = person
>     grid = SQLFORM.grid(db.dog.owner == person, args=request.args)
>     return {'grid':grid}
>
> however I am unclear as to how this default propagates in other 
> controllers or views. Do I run the risk of having "db.dog.owner.default" 
> defined in a context where I do not want it defined? 
>
> Any input as to what would be the recommended approach is welcome.
>
> Thanks
> Vincent
> (using web2py 2.0.8 from trunk)
>

-- 



Reply via email to