How to create a hidden field in CRUD?

20 views
Skip to first unread message

Marco

unread,
Aug 20, 2009, 7:21:55 PM8/20/09
to web2py-users
Hello everyone.

I am working with a controller for ratings on an item. The item
details come from a legacy database, and the ratings are stored in a
separate database. In the ratings database, alongside the ordinal
ratings I store the identifier of the item it refers to. I am using
the CRUD module to automagically generate the form I require. However,
I would like one particular field, the 'item_id' field, to be pre-
filled, invisible to and non-editable by the user. The item_id is the
first and only parameter to the ratings controller.

My current approach is to set the Field properties readable and
writable to False, and then modifying db.ratings.item_id.default with
each invocation to the controller. Is this a safe practice, or is it
possible that in a use case with two concurrent users, one user may
see the changes caused by the other user?

My next question is, is there a better way to do this? It almost feels
like there should be a hidden widget of some sort, but how would I
pass it the required value, since it varies according to the
parameters given to the controller? I am very new to this, so I may
have made some simple beginner mistakes and am very open to any design
suggestions.

I include a sketch of my controller below for reference.

Thanks in advance.

Cheers
Marco

@auth.requires_login()
def rate():
item_id = request.args(0)
target = db.item[item_id]

# target not valid, redirect to summary
if not target: redirect(URL(r=request, f='summary'))

ratings = db.ratings
ratings.item_id.default = target_id
prev = db((ratings.annotator_id==auth.user.id) &
(ratings.item_id==item_id)).select()
if len(prev) == 0:
form = crud.create(ratings, next=URL(r=request,f='summary'))
else:
form = crud.update(uq, prev[0], next=URL(r=request,f='summary'),
deletable=False)

return dict(form=form, target=target)

Iceberg

unread,
Aug 21, 2009, 2:52:46 AM8/21/09
to web2py-users
It is safe to modify db.yourtable.yourfield.default to different value
with each invocation to the controller. In web2py each definition are
excuted on the fly. It is an unusual design but really make things
funny.

Is there a better way to do this? I don't know one yet.

Regards,
Iceberg
Reply all
Reply to author
Forward
0 new messages