updating rows obtained via database.session.query()

11 views
Skip to first unread message

Terrence Brannon

unread,
May 12, 2015, 4:46:43 PM5/12/15
to nagare...@googlegroups.com
The NewContent.data() method retrieves database rows. The render
method for this class creates a form out of each row. When the form is
submitted the user can input a new value for the Content field of the
database row.

In ConfigureContent.answer() I attempt to update the database
row. However even though the database row is updated in memory, (1)
database.session does not know that the session is dirty and should be
commited (2) the database does not get updated.

How can I alter this code to update the database? I'm aware that I
could query the database and update, but that seems absolutely
wasteful as I already have the object here.

class NewContent(object):

    def __init__(self):
        pass

    def data(self):
        return database.session.query(
            models.OBFL_Log_ParsingErrors).filter(
                models.OBFL_Log_ParsingErrors.Type_Of_Record == 'Error')


    def config(self, comp, dbrow):
        print "Calling config"
        comp.becomes(ConfigureContent(dbrow))
        #r = comp.call(util.Ask("does she love me?"))
        #r = comp.becomes(util.Ask("does she love me?"))
        print "ask does finish?. "

@presentation.render_for(NewContent)
def render(self, h, comp, *args):

    for le in self.data()[:100]:
        #print "rowdict={0}".format(row)
        with h.div:
            with h.form.post_action(self.config, comp, le):
                h << h.input(type='Submit', value='Delete')
                h << h.input(value=le.File_ID, disabled=True)
                h << h.input(value=le.File_Name, disabled=True)
                h << h.input(value=le.Line_Start, disabled=True)
                h << h.input(value=le.Content, disabled=True)
                h << h.input(type='Submit', value='Configure')

    return h.root


class ConfigureContent(object):
    """Ask the user to enter a line of text

    The text entered is answered
    """
    def __init__(self, database_row):
        """Initialization

        In:
          - ``msg`` -- message to display
        """
        self.dbrow = database_row
        print "Config init complete"

    @staticmethod
    def cleancheck():
        print "Dirty check {0}".format(database.session.dirty)

    def answer(self, comp, r):
        self.cleancheck()
        print "R={0}. dbrowdict={1}".format(
            r(), self.dbrow.__dict__)
        self.dbrow.Content = r()
        print "R={0}. dbrowdict={1}".format(
            r(), self.dbrow.__dict__)
        self.cleancheck()

Alain Poirier

unread,
May 16, 2015, 10:06:00 AM5/16/15
to nagare...@googlegroups.com
> Le 12 mai 2015 à 22:46, Terrence Brannon <meta...@gmail.com> a écrit :
>
> The NewContent.data() method retrieves database rows. The render
> method for this class creates a form out of each row. When the form is
> submitted the user can input a new value for the Content field of the
> database row.
>
> In ConfigureContent.answer() I attempt to update the database
> row. However even though the database row is updated in memory, (1)
> database.session does not know that the session is dirty and should be
> commited (2) the database does not get updated.
>
> How can I alter this code to update the database? I'm aware that I
> could query the database and update, but that seems absolutely
> wasteful as I already have the object here.

I found the hooks to pickle the SQLAlchemy entities have changed in version >=0.9

So, to have your code running fine with a new version of SQLAlchemy you can:

- downgrade the version of SQLAlchemy: easy_install -U 'sqlalchemy<0.9'

or

- upgrade Nagare dev. to fetch my latest commit: easy_install -U nagare
Reply all
Reply to author
Forward
0 new messages