On 9/22/2010 5:59 PM, Michael Bayer wrote:
Anyway as I said, read through the columns on table.c and use 
default/server_default.

Just to put a ribbon around this, here's the gist of code that will empty a record (set it to defaults). Didn't take long once I knew where to look for the answers.

    from sqlalchemy.schema import ColumnDefault

    def cancel_car(self, lane):
        # list of columns that should not be emptied
        preserve = ('id_', 'lane', 'hist', 'auct_id', 'version_id')
        for col, val in self.col_defaults():
            if col not in preserve:
                setattr(lane, col, val)
        self.session.commit()
        return lane

    def col_defaults(self):
        """Returns a list of tuples of (col, default) that can be used to
        set any number of columns to their default value as defined in the
        SQLAlchemy model.
        """
        defaults = []  # begin list of tuples
        for col in Car.__table__.c:
            default = col.default
            if isinstance(default, ColumnDefault):
                default = default.arg
            defaults.append((col.description, default))
        return defaults

Again, thanks for all your help!

Michael

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to