Django + Postgres:字符串文字不能包含NUL(0x00)字符 [英] Django + Postgres: A string literal cannot contain NUL (0x00) characters

查看:874
本文介绍了Django + Postgres:字符串文字不能包含NUL(0x00)字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在同步大量数据,但又收到此错误:字符串文字不能包含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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆