3
有没有任何方式访问当前用户对象或至少没有访问请求的ID?模型中说呢?金字塔得到当前用户/用户ID无需访问请求
我使用金字塔和SQLAlchemy。
首先我想到了Singleton,但我认为这可能会在有更多的用户时返回错误的对象。
class UserSingleton(object):
user = None
def __new__(cls, user=None):
UserSingleton.user = user
return UserSingleton.user
@classmethod
def get(cls):
return cls.user
我需要这与我的模型中使用,但我不能在这里传递的要求,因为这是由ORM事件称为:
@classmethod
def log(cls, description, log_type=None, name=None):
user = ??
audit_log = cls(
user_id=user.id if user else None,
description=description,
type=log_type,
name=name
)
DBSession.add(audit_log)
return audit_log
我不认为数据库事件中存在当前用户,因为它们发生在请求的上下文之外。 –
在您的函数中明确传递'request'或'user'的最佳实践,而不是依赖于线程本地。 –