django从管理界面添加数据时:admin / ...的typeError ... unicode对象不可调用

时间:2011-09-20 02:49:34

标签: python django

尝试使用管理界面保存到以下模型时出错:

models.py

class Answer(models.Model):
a = models.TextField(primary_key=True)
gloss = models.TextField(blank=True)  
clean = models.TextField(blank=True)  
count = models.IntegerField(blank=True)
p = models.IntegerField(blank=True)    
def __unicode__(self):
    return u"%s" % self.a    
class Meta:                                       
    db_table = u'answers'                         

这是管理界面上显示的错误消息:

Environment:


Request Method: POST
Request URL: http://localhost:8000/admin/emotions/answer/add/

Django Version: 1.4 pre-alpha SVN-16322
Python Version: 2.6.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'emo20qBrowser.emotions']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/home/abe/bin/django-trunk/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args,     **callback_kwargs)
File "/home/abe/bin/django-trunk/django/contrib/admin/options.py" in wrapper
  316.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/abe/bin/django-trunk/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/home/abe/bin/django-trunk/django/views/decorators/cache.py" in _wrapped_view_func
  77.         response = view_func(request, *args, **kwargs)
File "/home/abe/bin/django-trunk/django/contrib/admin/sites.py" in inner
  196.             return view(request, *args, **kwargs)
File "/home/abe/bin/django-trunk/django/utils/decorators.py" in _wrapper
  25.             return bound_func(*args, **kwargs)
File "/home/abe/bin/django-trunk/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/home/abe/bin/django-trunk/django/utils/decorators.py" in bound_func
  21.                 return func(self, *args2, **kwargs2)
File "/home/abe/bin/django-trunk/django/db/transaction.py" in inner
  211.                 return func(*args, **kwargs)
File "/home/abe/bin/django-trunk/django/contrib/admin/options.py" in add_view
  871.             if form.is_valid():
File "/home/abe/bin/django-trunk/django/forms/forms.py" in is_valid
  121.         return self.is_bound and not bool(self.errors)
File "/home/abe/bin/django-trunk/django/forms/forms.py" in _get_errors
  112.             self.full_clean()
File "/home/abe/bin/django-trunk/django/forms/forms.py" in full_clean
  269.         self._post_clean()
File "/home/abe/bin/django-trunk/django/forms/models.py" in _post_clean
  331.             self.instance.clean()

Exception Type: TypeError at /admin/emotions/answer/add/
Exception Value: 'unicode' object is not callable

1 个答案:

答案 0 :(得分:2)

好吧,我想我想出来了......我正在使用一个名为“clean”的变量/列。 Django的管理界面也有一个名为“clean()”的方法,它可以进行某种验证。似乎存在某种命名冲突,所以我将变量更改为“clean”,然后确保它知道要使用的数据库字段(我使用的是遗留/预先存在的数据库),我添加了一个db_column选项:

cleaned = models.TextField(blank=True,db_column="clean")

很高兴知道“干净”是django中的保留标识符,但至少我只浪费了半天这个django的东西,表面上使数据库操作更容易。公平地说,我今天早上刚刚启动了django,如果我能在stackoverflow上找到并回答它,那将是一件轻而易举的事情。

如果有人知道更好的方法来解决这个问题,请告诉我......