views:

30

answers:

1

I have a table which is being mapped with SqlAlchemy. In that table is a UUID column. When I try to query that table, I get the uuid in bytes_le format. Is there some way I can tell the mapper to return a clean string representation instead? Code for the mapper is:

Practice = Table('Practice',metadata,
                 schema='pulse',autoload=True, autoload_with=engine, quote_schema=True)
class PracticeMap(object):
    def __str__(self):
        return "%s" % self.Name
    def __repr__(self):
        return "Name: %s, UUID: %s" % (self.Name, self.uuid)
mapper(PracticeMap,Practice)

Thanks.

+1  A: 

You can always reformat the uuid using the python uuid library:

import uuid
uuid_string = str(uuid.UUID(bytes_le=self.uuid))

If you only need the string representation for __repr__ that should do the trick. If you want the uuid property of your object to live in string-land, you'll want to rename the column property and provide your own uuid property to wrap it.

Seamus Campbell
I was hoping to avoid that, as I am passing a SqlAlchemy Query object to another function (using the SqlAlch field extensions for WTForms).
Jimmy McCarthy