Django + Postgres:字符串文字不能包含NUL(0x00)字符 [英] Django + Postgres: A string literal cannot contain NUL (0x00) characters
问题描述
我正在同步大量数据,但又收到此错误:字符串文字不能包含NUL(0x00)字符。
显然,这是一个postgres问题,但我不太确定如何解决。有没有办法在Django模型级别去除空字符?我有很多要同步的字段。
I'm syncing a large amount of data and I'm getting this error back: A string literal cannot contain NUL (0x00) characters.
Obviously this is a postgres problem, but I'm not quite sure how to solve it. Is there a way to strip null characters out at the Django model level? I have a large set of fields that I'm syncing.
推荐答案
此错误修复建议:
s.decode("utf-8", errors="replace").replace("\x00", "\uFFFD")
对于OP的发行,仅需 .replace
,即用``字符替换空值。我也包含了 .decode
,因为它可以防止类似情况下出现的其他编码问题。
Only the .replace
is necessary for the OP's issue, which replaces the null with a � character. I've included .decode
too as it protects against other encoding issues that might arise in similar situations.
将在某个位置的 .clean
方法中使用-如果要在全局应用它,可以子类TextField或CharField。
This would go in a .clean
method somewhere - maybe subclass TextField or CharField if you want to apply it globally.
这篇关于Django + Postgres:字符串文字不能包含NUL(0x00)字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!