On Fri, Aug 26, 2016 at 2:24 AM, Robert Minsk <rmi...@gmail.com> wrote:
> I am using automap_base to reflect a table.
>
> metadata = MetaData()
> metadata.reflect(bind=engine, only=['hosts'])
> automap = automap_base(metadata=metadata)
> automap.prepare()
>
> Hosts = automap.classes.hosts
>
> The mac_address column in Hosts is  binary and would like to to replace the
> mac_address with some sort of getter/setter to decode/encode the address.  I
> have not found an example of doing this with an automapped table.  Is there
> a easy way to do this?

You can override specific columns on an automapped table by
pre-declaring the class:

  
http://docs.sqlalchemy.org/en/latest/orm/extensions/automap.html#specifying-classes-explicitly

I haven't tried it, but I would assume that you can use any of the
normal facilities for modifying column behaviour at that point. For
example, I think you could do something like this:

class Hosts(automap):
    __tablename__ = 'hosts'
    _mac_address = Column('mac_address', BINARY)
    @property
    def mac_address(self):
        return decode_mac_address(self._mac_address)

    @mac_address.setter
    def mac_address(self, value):
        self._mac_address = encode_mac_address(value)

If you want to be able to query using the decoded values, you could
also try hybrid properties.

Hope that helps,

Simon

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to