Django 存储非 unicode 数据
我正在尝试使用 Django 将 IP 数据包有效负载存储在 PostgreSQL 数据库中。
目前,我将有效负载描述为 CharField。
我收到此错误:
django.db.utils.DatabaseError: invalid byte sequence for encoding "UTF8": 0xedbc93
HINT: This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".
有什么方法可以合理地存储这些数据吗?我可以毫无错误地执行 str(packet.payload)
,但是当 Django 尝试保存对象时,它会抛出编码错误。字节串似乎是显而易见的解决方案,但 Django 似乎并不支持它。
I'm attempting to store IP packet payloads in a PostgreSQL database with Django.
Currently, I'm storying the payload as a CharField.
I'm getting this error:
django.db.utils.DatabaseError: invalid byte sequence for encoding "UTF8": 0xedbc93
HINT: This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".
Is there any way to sanely store this data? I'm able to do str(packet.payload)
with no errors, but when Django tries to save the object it throws the encoding error. A bytestring seems like the obvious solution, but it doesn't look like Django supports that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你想存储任意字节串,你应该这样声明它们。许多(大多数?)字节序列不是有效的 UTF-8,因此这不是存储它们的好方法。 CharField 用于存储文本,而您没有文本。
这个问题的答案可能会有所帮助:Django Blob 模型字段
If you want to store arbitrary bytestrings, you should declare them as such. Many (most?) sequences of bytes are not valid UTF-8, so it isn't a good way to store them. A CharField is for storing text, and you don't have text.
The answers to this question will likely be helpful: Django Blob Model Field