Found my own answer: I can create an onvalidation function for the
form that only runs when the form is accepted but before the new
record is made. The problem this then introduces is how to set the
enclosing variable from the function without use of nonlocal. Guess I
can just create a dict for the vars ahead of time and set that from
the function. Like so:

    vars = {}

    def get_uuid(form):
        from uuid import uuid4
        uuid = uuid4()
        db.job_post.uuid.default = lambda: uuid
        vars['uuid'] = uuid

    if form.accepts(request.vars, session, onvalidation=get_uuid):
        vars['poster_name'] = form.vars.poster_name
        vars['poster_email'] = form.vars.poster_email
        redirect(URL('post_email', vars=vars))

On Mar 26, 6:01 pm, Brian Will <brian.thomas.w...@gmail.com> wrote:
> Thanks, that's a start, but how now do I get 'uuid' which I've set as
> a default field in the model. Is there a way I can manually set the
> value in the controller before invoking form.accepts? Simplest I can
> think of is:
>
> value = computevalue()
> db.table.field.default = lambda: value   # effectively manually set
> field to value
> if form.accepts(request.vars, session):
>     redirect(URL('bla', args={'value': value}))
>
> This way I have the field value so I can pass it on in the redirect.
>
> A bit odd, and I also don't like how I end up computing the value even
> when the form doesn't get accepted. So I'm wondering if there's a more
> direct way.
>
> BTW, is there any effective difference here if I use compute instead
> of default? Is the only real difference between the two that compute
> receives the other fields as a dict? In this dict passed to compute,
> are fields set by default included while compute fields are not? I
> guess with no guarantee is made about the order in which compute
> functions are invoked, so we can't add the computed field to this dict
> and expect it to be seen by other compute fields, right?
>
> On Mar 26, 5:09 pm, DenesL <denes1...@yahoo.ca> wrote:
>
>
>
>
>
>
>
> > The new record id will be in form.vars.id after the accepts.
>
> > On Mar 26, 7:46 pm, Brian Will <brian.thomas.w...@gmail.com> wrote:
>
> > > When using SQLFORM, I'd like to get the default/computed values of
> > > fields not included in the form. Can I do this without making another
> > > query? Can I get, say, the autogen'd id of the new record? For
> > > example:
>
> > > form = SQLFORM(db.job_post, submit_button='Post Job',
> > > formstyle='table2cols',
> > >         fields=['poster_name', 'poster_email', 'poster_phone',
> > > 'zipcode', 'location_description', 'job_type', 'job_title',
> > > 'job_description'],
> > >         _id='postjob'
> > > )
> > > if form.accepts(request.vars, session):
> > >     # ...
>
> > > I want the 'id' and 'uuid' field values created when this form
> > > accepts. To be honest, I'm not even sure how to do this with a query
> > > except by matching on all provided fields (because none of the other
> > > fields are unique), and that just feels ugly.
>
> > > Should I have to resort to a manual FORM in this instance?
>
> > > Thanks.

Reply via email to