TextField在Django中使用unicode

时间:2011-01-22 20:31:32

标签: django unicode django-models

我正在尝试将包含序号不在范围(128)中的字符的字符串保存到数据库中。该字段在模型中声明为TextField,当我调用save()方法时,抛出了异常:

File "/Library/Python/2.6/site-packages/django/db/models/base.py", line 434, in save self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/Library/Python/2.6/site-packages/django/db/models/base.py", line 500, in save_base rows = manager.using(using).filter(pk=pk_val)._update(values)
File "/Library/Python/2.6/site-packages/django/db/models/query.py", line 491, in _update return query.get_compiler(self.db).execute_sql(None)
File "/Library/Python/2.6/site-packages/django/db/models/sql/compiler.py", line 861, in execute_sql cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/Library/Python/2.6/site-packages/django/db/models/sql/compiler.py", line 717, in execute_sql sql, params = self.as_sql()
File "/Library/Python/2.6/site-packages/django/db/models/sql/compiler.py", line 826, in as_sql val = field.get_db_prep_save(val, connection=self.connection)
File "/Library/Python/2.6/site-packages/django/db/models/fields/subclassing.py", line 28, in inner return func(*args, **kwargs)
File "/Library/Python/2.6/site-packages/django/db/models/fields/subclassing.py", line 28, in inner return func(*args, **kwargs)
File "/Library/Python/2.6/site-packages/django/db/models/fields/__init__.py", line 276, in get_db_prep_save return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/Library/Python/2.6/site-packages/django/db/models/fields/subclassing.py", line 53, in inner return func(*args, **kwargs)
File "/Library/Python/2.6/site-packages/django/db/models/fields/__init__.py", line 652, in get_db_prep_value value = self.get_prep_value(value)
File "/Library/Python/2.6/site-packages/django/db/models/fields/__init__.py", line 647, in get_prep_value return self.to_python(value)
File "/Library/Python/2.6/site-packages/django/db/models/fields/__init__.py", line 610, in to_python if not ansi_date_re.search(value):
TypeError: expected string or buffer

这是我做的:

str_unicode = str.encode('utf-8')
m = MyModel.new(str = str_unicode)
m.save()

那么我该怎么做才能启用unicode兼容性?

更新:我目前正在使用sqlite3进行开发,python 2.6.1和Mac上的Django 1.2.4

4 个答案:

答案 0 :(得分:3)

utf-8 不是 unicode!这一行:

str_unicode = str.encode('utf-8')

与你想要的相反。它需要unicode,并将其转换为utf-8。不要这样做。

答案 1 :(得分:2)

你的问题根本不在于TextField。如果查看回溯中引用的代码,则会在DateField的{​​{1}}方法中触发异常。你可以see the relevant code here。基本上看起来你传递了一些不合适的东西作为模型中另一个字段的输入。

需要学习的经验教训:请务必仔细阅读TRACEBACKY!

作为旁注,如果你真的想在Python中强制使用unicode,请调用内置to_python类型:

unicode

使用str_unicode = unicode(str) 方法转换为UTF-8可以完全不同。

答案 2 :(得分:0)

Django可以正常使用unicode。

但如果您的输入未正确转换,您将遇到麻烦。 作为一个例子:我在一个错误的PHP脚本错误地填充旧的MyISAM数据库时遇到了很多麻烦。 检查你的输入。

您使用什么数据库后端?什么级别的python?

答案 3 :(得分:0)

原因是我没有在django的数据库配置中加载mysql设置,这与我提出的unicode问题无关。

配置应该是这样的:

   DATABASES = {
       'default': {
           'ENGINE': 'django.db.backends.mysql', 
           'NAME': 'db',
           'USER': 'xxx',
           'PASSWORD': 'xxx', 
           'HOST': 'localhost',
           'PORT': '3306', 
           'OPTIONS' : {
              "init_command": "SET storage_engine=INNODB",
              'read_default_file': '/etc/my.cnf',
           }
       }
   }