Why not using web2py feature for this :

db[request.args(0)].entered_by.update=auth.user and auth.user.id
db[request.args(0)].input_date.update=request.now

request.args(0) = your table

So here you tell web2py to stamp user id (from auth_user) into entered_by
field of the given table only on update... You can define also the default
at the models level...

Richard

On Thu, Mar 10, 2011 at 9:47 AM, DenesL <[email protected]> wrote:

>
> After an insert, form.vars.id will have the new record's id.
>
> But to avoid another DB access you can make the changes before the
> record is written using the onvalidation parameter in the accepts.
>
> @auth.requires_login()
> def new():
>    def set_name(form):
>        form.vars.name=author
>        return
>     form = SQLFORM(db.autos, fields=['num'],
>           labels={'num':'Number'},
>           submit_button='GO',
>           formstyle='divs')
>     if form.accepts(request.vars, session,
>            onvalidation=set_name):
>         response.flash = 'Yoh-ho-ho!'
>    elif form.errors:
>        response.flash = 'Bad-bad-bad!'
>     else:
>        response.flash = 'Fill the form'
>    return dict(form=form)
>
>
> On Mar 10, 5:56 am, cyber <[email protected]> wrote:
> > The code in controller:
> > @auth.requires_login()
> > def new():
> >     author=auth.user.username
> >     form = SQLFORM(db.autos, fields=['num'], labels={'num':'Number'},
> > submit_button='GO', formstyle='divs')
> >     if form.accepts(request.vars, session):
> >         response.flash = 'It's OK!'
> >     elif form.errors:
> >         response.flash = 'There is an error!'
> >     else:
> >         response.flash = 'Fill the form!'
> >     rows=db(db.autos.id>0).select()
> >     last_row=rows[-1]
> >     last_row.update_record(name=author)
> >     return dict(form=form)
> >
> > *******************************
> > And the model piece of code is:
> > import datetime
> > now=datetime.datetime.today()
> > db.define_table('autos',
> >     Field('num', length=8),
> >     Field('name'),
> >     Field('dtime', 'datetime', default=now),
> >     Field('enter', 'boolean', default=False),
> >     Field('enter_d'),
> >     Field('idk', 'boolean', default=False),
> >     Field('idk_d'),
> >     Field('svh', 'boolean', default=False),
> >     Field('svh_d'),
> >     Field('reg', 'boolean', default=False),
> >     Field('reg_d'),
> >     Field('out', 'boolean', default=False),
> >     Field('out_d')
> >     )
> >
> > *****************
> > And view file is:
> > {{extend 'layout.html'}}
> > {{=form}}
> >
> > So, I want user to enter only value 'num' in db.autos.
> > But user_name (for current user) and date_time info have to be updated
> > automaticly after user presses "GO" button.
> > Could you check my code? Is it correct?
> >
> > On 9 мар, 17:27, DenesL <[email protected]> wrote:
> >
> > > Can you show us your current code?.
> >
> > > On Mar 9, 9:03 am, cyber <[email protected]> wrote:
> >
> > > > Hi
> >
> > > > How can I update new (just created) record in the table?
> > > > Table consists of several fields but in the form page user have to
> > > > enter only one value for one row.
> > > > So, I need insert in the current row not only this value but user
> name
> > > > and datetime.
> > > > I can insert first value but I have no idea of how to auto update new
> > > > row with required values.
> >
> > > > Any ideas?
> >
> >
>

Reply via email to