Most of the time in web2py you don't need to commit, but in particular
scenario you maybe need to commit before an other operation because you
need the database to be up to date before the and of your function.

http://web2py.com/books/default/chapter/29/6?search=db.commit%28%29

Hope it helps

On Fri, Mar 23, 2012 at 9:47 AM, Luis Díaz <[email protected]> wrote:

> The following code gives me error if not explicitly use db.commit ()
> can someone explain why I now need to use db.commit () to something so
> simple?
>
> models/home.py
>
> ##############
> id_user = (auth.user and auth.user.id) or None
>
> db.define_table('home',
>    Field('titulo', 'string', length=64, default='titulo de prueba'),
>    Field('texto', 'text', length=5000, default='texto de prueba'),
>    Field('user', db.auth_user, default=id_user, writable=False,
> readable=False),
>
>    #auditoria
>    Field('ip', 'string', length=64,  default=request.client,
> update=request.client, writable=False, readable=False),
>    Field('fecha', 'date',default=request.now, update=request.now,
> writable=False, readable=False),
>    Field('user_update', db.auth_user, default=id_user,
> update=id_user, writable=False, readable=False),
> )
> ############
>
>
> controllers/default.py
> ##############
> def index():
>
>    if auth.is_logged_in() :
>        home = db.home(user=id_user)
>        if home is None: redirect(URL('update'))
>
>    return dict()
>
>
> @auth.requires_login()
> def update():
>
>    home = db.home(user=id_user)
>
>    if home is None:
>        home_id = db.home.insert(user=id_user)
>    else:
>        home_id = home.id
>
> #    db.commit()
>    form = crud.update(db.home, home_id, deletable=False,
> next=URL('update'))
>
>    return dict(home=home, form=form)
> #############
>
>
> view/default/update.html
>
> {{extend 'layout.html'}}
>
>    <div>
>        <span id="span_home_titulo">{{ =home.titulo }}</span>
>    </div>
>    <div id="form_home" >
>        {{=form.custom.begin}}
>            {{ =form.custom.widget.titulo }}
>            {{ =form.custom.widget.texto }}
>            <input type="submit" value="Save" />
>        {{=form.custom.end}}
>    </div>
>
> ###############
>
>
> Error
> #########
>
> web2py™ (1, 99, 7, datetime.datetime(2012, 3, 4, 22, 12, 8), 'stable')
> Python  Python 2.6.6: /usr/bin/python
> TRACEBACK
>
> Traceback (most recent call last):
>  File "/home/diazluis2007/web2py/gluon/restricted.py", line 205, in
> restricted
>    exec ccode in environment
>  File
> "/home/diazluis2007/web2py/applications/about/views/default/update.html",
> line 80, in <module>
> AttributeError: 'NoneType' object has no attribute 'titulo'
>
>
>
> Díaz Luis
> Analista Programador Facultad de Odontología UC
> http://www.about.me/diazluis
> User Linux 532223
>

Reply via email to