可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
How do you store a "blob" of binary data using Django's ORM, with a PostgreSQL backend? Yes, I know Django frowns upon that sort of thing, and yes, I know they prefer you use the ImageField or FileField for that, but suffice it to say, that's impractical for my application.
I've tried hacking it by using a TextField, but I get occassional errors when my binary data doesn't strictly confirm to the models encoding type, which is unicode by default. e.g. psycopg2.DataError: invalid byte sequence for encoding "UTF8": 0xe22665
回答1:
This snippet any good: This is possibly the simplest solution for storing binary data in a TextField. import base64 from django.db import models class Foo(models.Model): _data = models.TextField( db_column='data', blank=True) def set_data(self, data): self._data = base64