Xml SQLAlchemy TypeDecorator不';行不通
我正在postgresql数据库中使用Xml SQLAlchemy TypeDecorator不';行不通,xml,postgresql,sqlalchemy,elementtree,Xml,Postgresql,Sqlalchemy,Elementtree,我正在postgresql数据库中使用xml,我需要一个自定义类型,可以在SQLAlchemy中处理xml数据 因此,我创建了一个与xml.etree通信的XMLType类,但它并没有像我希望的那样工作 这是我写的代码: import xml.etree.ElementTree as etree class XMLType(sqlalchemy.types.TypeDecorator): impl = sqlalchemy.types.UnicodeText type = e
xml
,我需要一个自定义类型,可以在SQLAlchemy中处理xml
数据
因此,我创建了一个与xml.etree
通信的XMLType
类,但它并没有像我希望的那样工作
这是我写的代码:
import xml.etree.ElementTree as etree
class XMLType(sqlalchemy.types.TypeDecorator):
impl = sqlalchemy.types.UnicodeText
type = etree.Element
def get_col_spec(self):
return 'xml'
def bind_processor(self, dialect):
def process(value):
if value is not None:
return etree.dump(value)
else:
return None
return process
def process_result_value(self, value, dialect):
if value is not None:
value = etree.fromstring(value)
return value
它在检索值和结果处理方面效果很好。但是当我尝试插入一行时,我得到了一个错误(当然,我将主体
放为xml.etree.ElementTree.Element
对象):
看到body
值为None
,显然绑定处理器工作不正常,但我认为我正确地实现了这一点,因此我不知道该如何改变这种情况
process\u bind\u param
给我同样的错误
我的代码哪里出错了?
ElementTree.dump()
函数将XML转储到流(默认情况下为标准输出),并返回None
。使用ElementTree.tostring()
或将其转储到StringIO
对象。谢谢。这是我的无知,不是炼金术的问题。:-)哦,我不知道有“接受答案”功能。我就这么做了。谢谢你的考虑。
IntegrityError: (IntegrityError) null value in column "body" violates not-null
constraint "INSERT INTO comments (id, author_id, look_id, body, created_at)
VALUES (nextval('object_seq'), %(author_id)s, %(look_id)s, %(body)s, now())
RETURNING comments.id" {'body': None, 'author_id': 1550L, 'look_id': 83293L}