yes but here is the most canonical approach:
import uuid
from sqlalchemy import Column
from sqlalchemy import event
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class HasUUID(object):
id = Column(UUID(), default=uuid.uuid4, primary_key=True)
@event.listens_for(HasUUID, "init", propagate=True)
def init(obj, arg, kw):
obj.id = uuid.uuid4()
class User(HasUUID, Base):
__tablename__ = 'user'
u1 = User()
print(
u1.id)
Alternatively, you can write an event that scans for columns that
contain a Python level default and invokes them. I think I did this
for someone once but it's apparently not in the wiki, but in any case
doing this using class-level inheritance is IMO the cleanest way since
your User class takes on the role of HasUUID explicitly.
>
> Much thanks!
> Jens
>
> --
> You received this message because you are subscribed to the Google Groups "sqlalchemy-alembic" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
sqlalchemy-alem...@googlegroups.com.
> For more options, visit
https://groups.google.com/d/optout.