Interesting, I didn't have to set id to None. Perhaps it's db
specific. I was using sqlite. Anyway, _filter_fields is better in
either case. Thanks!

On Dec 5, 7:00 am, villas <villa...@gmail.com> wrote:
> Hi Nathan
>
> Just a small point, I tried your code example and it seems you have to
> set id==None otherwise it tries to add a duplicate row with the same
> id.
>
> Anyway, I thought you also might like this alternative using
> _filter_fields, just because it's shorter. Maybe it's got other
> issues,  but it seems to work!
>
> def things():
>     form = SQLFORM(db.things)
>     if form.accepts(request.vars,session):
>         response.flash = 'Added a thing'
>     if request.vars.dupe:
>
> db.things.insert(**db.things._filter_fields(db.things(request.vars.dupe)))
>     things = db(db.things.id>0).select()
>     return dict(form=form,things=things)
>
> Best regards,
> -D
>
> On Dec 4, 3:30 am, "mr.freeze" <nat...@freezable.com> wrote:
>
> > Here is an example:
>
> > Model
> > -----
> > db.define_table('things',Field('name',requires=IS_NOT_EMPTY()),
> >                 Field('age','integer'),Field('weight','double'))
> > db.things.id.represent = lambda v: A(v,_href=URL(vars=dict(dupe=v)),
> >                                      _onclick='return confirm("Copy
> > %s?")' % v)
>
> > Controller
> > ----------
> > def index():
> >     form = SQLFORM(db.things)
> >     if form.accepts(request.vars,session):
> >         response.flash = 'Added a thing'
> >     if request.vars.dupe:
> >         record = db.things(request.vars.dupe)
> >         vals = {}
> >         for k,v in record.items():
> >             if k in db.things.fields:
> >                 vals[k] = v
> >         db.things.insert(**vals)
> >     things = db(db.things.id>0).select()
> >     return dict(form=form,things=things)
>
> > On Dec 3, 6:12 pm, tomt <tom_tren...@yahoo.com> wrote:
>
> > > Hi,
>
> > > I've started to write a simple application to learn web2py. I am using
> > > SQLFORM to insert, modify and delete records. So far everything is
> > > working as planned.
>
> > > I would like to give the user the ability add a new record as a copy
> > > of an existing one.  The idea is that the user would view an existing
> > > record, modify a couple fields, and select 'copy' which result in
> > > adding a new record.
>
> > > I assume that this requires additional code, and I was hoping that
> > > someone would suggest an example, or let me know if SQLFORM can do
> > > this automatically.
>
> > > Thanks in advance,
>
> > > - Tom
>
>

Reply via email to