25👍
This bug fix suggests:
s.decode("utf-8", errors="replace").replace("\x00", "\uFFFD")
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.
This would go in a .clean
method somewhere – maybe subclass TextField or CharField if you want to apply it globally.
2👍
Unless you definitely do want to store NUL characters, you should sanitize your text so it does not contain them. At the model level, you’d define a clean_fieldname
method to do that.
If you do want to store them, you need to store them in a binary-compatible field in the database. Django 1.6+ has BinaryField
which should work.
- Django 'resolve' : get the url name instead of the view_function
- How to assign to a Django PointField model attribute?
- What is the different between the get logger functions from celery.utils.log and logging?
- How to make follower-following system with django model
Source:stackexchange.com