在SQLAlchemy中使用Postgresql xml数据类型

编程入门 行业动态 更新时间:2024-10-25 02:29:31
本文介绍了在SQLAlchemy中使用Postgresql xml数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

SqlAlchemy通过方言支持大多数特定于数据库的数据类型,但是我找不到与postgresql xml列类型一起使用的任何东西。有人知道可行的解决方案吗?理想情况下,它不需要我自己执行自定义列类型。

SqlAlchemy supports most database specific data types via dialects, but I could not find anything to work with the postgresql xml column type. Does somebody know a working solution. Idealy it should not require a custom column type implementation by myself.

推荐答案

如果需要使用 native'xml'数据类型在Postgresql数据库中,您需要编写自 UserDefinedType 而不是从TypeDecorator继承的自定义类型。 文档

If you need to have native 'xml' data type in postgresql database, you need to write custom type which inherited from UserDefinedType not from TypeDecorator. Documentation

这是我在其中一个项目中使用的:

Here is what I used in one of the projects:

import xml.etree.ElementTree as etree import sqlalchemy class XMLType(sqlalchemy.types.UserDefinedType): def get_col_spec(self): return 'XML' def bind_processor(self, dialect): def process(value): if value is not None: if isinstance(value, str): return value else: return etree.tostring(value) else: return None return process def result_processor(self, dialect, coltype): def process(value): if value is not None: value = etree.fromstring(value) return value return process

更多推荐

在SQLAlchemy中使用Postgresql xml数据类型

本文发布于:2023-10-16 16:51:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498163.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据类型   SQLAlchemy   Postgresql   xml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!