问题描述
我正在尝试两个新的东西,所以在简化和澄清的帮助是赞赏。
从sqlalchemy.ext .declarative import声明
从sqlalchemy导入列,浮点,事件
类TimeStampMixin(对象):
@declared_attr
def __tablename __(cls):
return cls .__ name __。lower()
created =列(Float)
modified =列(Float)
def __init __(self,created = None,
modified = None)
self.created = created
self.modified = modified
def create_time(mapper,connection,target):
target。 create = time()
#def modified_time(mapper,connection,target):
#target.modified = time()
event.listen(TimeStampMixin, 'before_insert',create_time)
#event.listen(TimeStampMixin,'before_update',modified_time)
所以我想创建一个mixin我可以在任何类中申请:
class MyClass(TimeStampMixin,Base):
等,etc,etc
此类继承了在创建时创建时间戳的功能,并在更新时创建/修改时间戳。 >
导入我收到此错误:
raise exc.UnmappedClassError(class_)
sqlalchemy.orm.exc.UnmappedClassError:类'db.database.TimeStampMixin'未映射
aaaand在这一点上我被困了。
这是我在 before_insert
事件:将 classmethod
添加到您注册当前类和句柄的 TimeStampMixin
设置创建时间。
例如
class TimeStampMixin(object)
#其他类方法
@staticmethod
def create_time(mapper,connection,targe t):
target.created = time()
@classmethod
def register(cls):
sqlalchemy.event.listen(cls,'before_insert' cls.create_time)
这样,你可以:
- 轻松扩展和更改您收听的内容以及您注册的内容。
- 覆盖某些类的create_time方法
- 明确说明哪些方法需要设置时间戳。
您可以简单地使用它:
class MyMappedClass(TimeStampMixin,Base):
pass
MyMappedClass.register()
简单,很清楚,没有魔法,但仍然像你想要的封装。
I am attempting 2 new things at once, so assistance in both simplifying and clarifying is appreciated.
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy import Column, Float, event
class TimeStampMixin(object):
@declared_attr
def __tablename__(cls):
return cls.__name__.lower()
created = Column(Float)
modified = Column(Float)
def __init__(self, created = None,
modified = None):
self.created = created
self.modified = modified
def create_time(mapper, connection, target):
target.created = time()
#def modified_time(mapper, connection, target):
# target.modified = time()
event.listen(TimeStampMixin, 'before_insert', create_time)
#event.listen(TimeStampMixin, 'before_update', modified_time)
So I want to create a mixin I can apply in any class:
class MyClass(TimeStampMixin, Base):
etc, etc, etc
This class inherits functionality that creates a timestamp on creation and creates/modifies a timestamp on update.
on import I get this error:
raise exc.UnmappedClassError(class_)
sqlalchemy.orm.exc.UnmappedClassError: Class 'db.database.TimeStampMixin' is not mapped
aaaand I'm stumped at this point.
Here's what I'd do to listen on before_insert
events: add a classmethod
to your TimeStampMixin
that registers the current class and handles setting creation time.
E.g.
class TimeStampMixin(object):
# other class methods
@staticmethod
def create_time(mapper, connection, target):
target.created = time()
@classmethod
def register(cls):
sqlalchemy.event.listen(cls, 'before_insert', cls.create_time)
That way, you can:
- Easily extend and change what you listen for and what you register.
- Override the create_time method for certain classes
- Be explicit about which methods need to have their timestamps set.
You can use it simply:
class MyMappedClass(TimeStampMixin, Base):
pass
MyMappedClass.register()
Simple, very clear, no magic, but still encapsulates like you want.
这篇关于Sqlalchemy mixins /和事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!