6
A
回答
6
这是最简单的,只是通过属性重命名映射列,并代理其:
class Something(Base):
...
_foo = Column('foo', String(123))
@property
def foo(self):
return self._foo
@foo.setter
def foo(self, value):
if len(value) > _foo.type.length:
raise Exception("Value too long")
self._foo = value
您可以轻松地分解出的创造,甚至使用类似formencode一个通用的验证框架。
如果你需要一个更SQLAlchemy的具体的解决方案,而且不介意使用特定的接口,然后SQLAlchemy的已经有属性的捕捉事件的扩展机制。使用验证器会是这个样子:
from sqlalchemy.orm.interfaces import AttributeExtension, InstrumentationManager
from sqlalchemy.orm import ColumnProperty
class InstallValidatorListeners(InstrumentationManager):
def post_configure_attribute(self, class_, key, inst):
"""Add validators for any attributes that can be validated."""
prop = inst.prop
# Only interested in simple columns, not relations
if isinstance(prop, ColumnProperty) and len(prop.columns) == 1:
col = prop.columns[0]
# if we have string column with a length, install a length validator
if isinstance(col.type, String) and col.type.length:
inst.impl.extensions.insert(0, LengthValidator(col.type.length))
class ValidationError(Exception):
pass
class LengthValidator(AttributeExtension):
def __init__(self, max_length):
self.max_length = max_length
def set(self, state, value, oldvalue, initiator):
if len(value) > self.max_length:
raise ValidationError("Length %d exceeds allowed %d" %
(len(value), self.max_length))
return value
你会然后你想验证任何类设置__sa_instrumentation_manager__ = InstallValidatorListeners
使用这个扩展。如果您希望它适用于从它派生的所有类,您也可以将其设置在基类上。
0
这里有一个更新的版本,适合新的SQLAlchemy版本的事件系统:
class InstallValidatorListeners(InstrumentationManager):
def post_configure_attribute(self, class_, key, inst):
"""Add validators for any attributes that can be validated."""
prop = inst.prop
# Only interested in simple columns, not relations
if isinstance(prop, ColumnProperty) and len(prop.columns) == 1:
col = prop.columns[0]
if isinstance(col.type, String) and col.type.length:
sqlalchemy.event.listen(
getattr(class_, key), 'set', LengthValidator(col.type.length), retval=True)
class ValidationError(Exception):
pass
class LengthValidator(AttributeExtension):
def __init__(self, max_length):
self.max_length = max_length
def __call__(self, state, value, oldvalue, initiator):
if len(value) > self.max_length:
raise ValidationError(
"Length %d exceeds allowed %d" % (len(value), self.max_length))
return value
相关问题
- 1. SQLAlchemy 0.7 - 最大列长度
- 2. 列出最大长度和最大长度的所有SQL列
- 3. LPAD列的最大长度
- 4. Rails ActiveAdmin最大列长度
- 5. 阵列最大长度
- 6. 最大长度
- 7. NSURL最大长度?
- 8. CSS最大长度
- 9. UITextView最大长度
- 10. 最大URL长度
- 11. CString最大长度
- 12. 最大NVARCHAR长度
- 13. 与最大长度
- 14. BindingSource.Filter最大长度
- 15. TextArea最大长度?
- 16. JAVA_TOOL_OPTIONS最大长度
- 17. MaskedTextBox最小/最大长度
- 18. URL片段(散列)的最大长度
- 19. 到休眠列的最大长度
- 20. Excel 2003:最大长度下拉列表
- 21. 最大阵列长度限额
- 22. SQL最大长度(总合)多列
- 23. 检查表列已达最大长度
- 24. MySQL中的最大JSON列长度
- 25. scala队列的最大长度
- 26. WCF 16384最大阵列长度
- 27. MySQL的:计数最大序列长度
- 28. 更改varchar列的最大长度?
- 29. 短信队列最大长度?
- 30. MySQL中列名的最大长度
请提供您所使用来定义表和映射到你的类的代码。 AFAIK,数据库应该引发一个通过SQLAlchemy传回的错误。请张贴代码提供一些关于您现在正在尝试的提示。 –
@ S.Lott MySQL在插入/更新时不检查字符串长度。它静静地截断太长的字符串。 – codeape