开发者

SqlAlchemy Mapper not returning clean UUIDs

开发者 https://www.devze.com 2023-01-09 21:20 出处:网络
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

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消