2011-09-20 58 views
2

我越來越想使用管理界面保存到下面的模型時的錯誤:Django的增加從管理界面數據時:在管理類型錯誤/ ... unicode的對象不是可調用

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 

回答

2

好吧,我想我想通了......我使用所謂的「乾淨」的變量/列。 Django的管理界面也有一個名爲「clean()」的方法,它進行某種驗證。它似乎有某種命名衝突,所以我將變量更名爲「清理」,然後確保它知道要使用的數據庫字段(我使用的是舊版/預先存在的數據庫),我添加了一個db_column選項:

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

這本來是很高興知道,「乾淨」是在Django的保留標識符,但至少我只是浪費了半天時間在這個Django的東西,這顯然是使數據庫操作更容易。公平起見,我今天早上剛開始django,如果我能找到並在stackoverflow上回答,它將是一件輕而易舉的解決。

如果有人知道一個更好的方式來處理這個問題,請讓我知道...

+1

無賴......這是令人沮喪的。以下是顯示模型的屬性/方法等的文檔頁面:https://docs.djangoproject.com/en/1.3/ref/models/instances/ – Aaron