我xml在我的postgresql数据库中使用,我需要一个自定义类型可以处理xmlSQLAlchemy中的数据.
所以我让XMLType课堂沟通xml.etree,但它没有按照我的意愿工作.
这是我写的代码:
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
Run Code Online (Sandbox Code Playgroud)
它适用于检索值和结果处理.但是当我试图插入一行时,我得到一个错误(当然,我把bodyas xml.etree.ElementTree.Element对象):
IntegrityError: (IntegrityError) null value in column "body" violates not-null
constraint "INSERT INTO comments (id, author_id, look_id, body, …Run Code Online (Sandbox Code Playgroud)