Why can update database records/objects in controllers but not in unrelated (noncontroller) functions?

8 views
Skip to first unread message

sebe...@spawar.navy.mil

unread,
May 1, 2008, 2:34:44 AM5/1/08
to TurboGears
It appears I can alter database records/objects just fine *within*
controllers but not *outside* of controllers.

I use turbogears.scheduler.add_weekday_task to invoke a function that
is not a controller. It appears this function invoked by
turbogears.scheduler.add_weekday_task cannot change database because
it isn't a controller.

Are there any gotchas/differences between touching database in a
controller and in an unrelated function?

chris

lilspikey

unread,
May 1, 2008, 4:46:37 AM5/1/08
to TurboGears


On May 1, 7:34 am, "seber...@spawar.navy.mil"
Inside a controller your code will be running in a transaction -
whereas elsewhere it won't. There's a bit of stuff here:

http://docs.turbogears.org/1.0/ModelOutsideTG

sebe...@spawar.navy.mil

unread,
May 1, 2008, 2:34:48 PM5/1/08
to TurboGears
Wow thanks!

Chris

sebe...@spawar.navy.mil

unread,
May 2, 2008, 3:10:13 AM5/2/08
to TurboGears


On May 1, 1:46 am, lilspikey <lilspi...@gmail.com> wrote:

> Inside a controller your code will be running in a transaction -
> whereas elsewhere it won't. There's a bit of stuff here:
>
> http://docs.turbogears.org/1.0/ModelOutsideTG

I printed that and read it. Can you please elaborate how you modify
database
outside of a controller? I'm still having problems.

cs

Cecil Westerhof

unread,
May 2, 2008, 5:02:00 AM5/2/08
to turbo...@googlegroups.com
2008/5/2 sebe...@spawar.navy.mil <sebe...@spawar.navy.mil>:
> Inside a controller your code will be running in a transaction -
> whereas elsewhere it won't.  There's a bit of stuff here:
>
> http://docs.turbogears.org/1.0/ModelOutsideTG

I printed that and read it.  Can you please elaborate how you modify
database
outside of a controller?  I'm still having problems.

A very simple example.
I have the following script:
#####
import turbogears

turbogears.update_config(configfile="dev.cfg",
                         modulename="tblog.config")

from tblog.model import Hutch

def displayHutch():
    print 'start displaying Hutch'
    for hutch in Hutch.select():
        print hutch.name
    print 'done displaying Hutch'

displayHutch()
for i in ['A', 'B']:
    for j in range(1, 6):
        Hutch(name = i + str(j))
displayHutch()
#####

and this gives the following output:
#####
start displaying Hutch
done displaying Hutch
start displaying Hutch
A1
A2
A3
A4
A5
B1
B2
B3
B4
B5
done displaying Hutch
#####

--
Cecil Westerhof

Jim Steil

unread,
May 2, 2008, 10:55:52 AM5/2/08
to turbo...@googlegroups.com
I made a quick nonsensical example.  I think this is what you're looking for...

http://paste.turbogears.org/paste/2637

    -Jim


sebe...@spawar.navy.mil

unread,
May 3, 2008, 1:41:43 AM5/3/08
to TurboGears
Jim

Thank you. I got it to work now.
I'm curious why controllers don't need an explicit commit() like
your script does. Perhaps TurboGears automatically does a silent
commit()
after every controller call?

Also, I assume your TurboGears apps have a line in your model file
like the following..

__connection__ = turbogears.database.PackageHub("myproject")

Why didn't you use this __connection__ reference in your example?
Perhaps, using a model object's _connection attribute is "safer"
because you are only exposing
a database connection for one class?

Also,

On May 2, 7:55 am, Jim Steil <st...@mailbag.com> wrote:

midtoad

unread,
May 3, 2008, 12:12:40 PM5/3/08
to TurboGears


On May 2, 11:41 pm, "seber...@spawar.navy.mil"
<seber...@spawar.navy.mil> wrote:

> Why didn't you use this __connection__ reference in your example?

I believe if you do that you will get a new connection made to the
database each and every time your scheduled job runs.
I
Reply all
Reply to author
Forward
0 new messages