当我尝试在我的django list_display
文件中创建admin.py
时,我一直收到以下错误消息。 Truncated incorrect DOUBLE value: 'Penguin Books'
class BookAdmin(admin.ModelAdmin):
list_display = 'title', 'publisher'
admin.site.register(Book, BookAdmin)
class Book(models.Model):
id = models.IntegerField(primary_key=True)
title = models.CharField(max_length=30L)
publisher = models.ForeignKey('Publisher', db_column='publisher')
class Meta:
db_table = 'book'
def __unicode__(self):
return self.name
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/app/books/
Django Version: 1.5.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'books')
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 "C:\Python27E\lib\site-packages\django\core\handlers\base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Python27E\lib\site-packages\django\contrib\admin\options.py" in wrapper
372. return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Python27E\lib\site-packages\django\utils\decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "C:\Python27E\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
89. response = view_func(request, *args, **kwargs)
File "C:\Python27E\lib\site-packages\django\contrib\admin\sites.py" in inner
202. return view(request, *args, **kwargs)
File "C:\Python27E\lib\site-packages\django\utils\decorators.py" in _wrapper
25. return bound_func(*args, **kwargs)
File "C:\Python27E\lib\site-packages\django\utils\decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "C:\Python27E\lib\site-packages\django\utils\decorators.py" in bound_func
21. return func(self, *args2, **kwargs2)
File "C:\Python27E\lib\site-packages\django\contrib\admin\options.py" in changelist_view
1285. 'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},
File "C:\Python27E\lib\site-packages\django\db\models\query.py" in __len__
90. self._result_cache = list(self.iterator())
File "C:\Python27E\lib\site-packages\django\db\models\query.py" in iterator
301. for row in compiler.results_iter():
File "C:\Python27E\lib\site-packages\django\db\models\sql\compiler.py" in results_iter
775. for rows in self.execute_sql(MULTI):
File "C:\Python27E\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
840. cursor.execute(sql, params)
File "C:\Python27E\lib\site-packages\django\db\backends\util.py" in execute
41. return self.cursor.execute(sql, params)
File "C:\Python27E\lib\site-packages\django\db\backends\mysql\base.py" in execute
120. return self.cursor.execute(query, args)
File "C:\Python27E\lib\site-packages\MySQLdb\cursors.py" in execute
176. if not self._defer_warnings: self._warning_check()
File "C:\Python27E\lib\site-packages\MySQLdb\cursors.py" in _warning_check
92. warn(w[-1], self.Warning, 3)
Exception Type: Warning at /admin/app/books/
Exception Value: Truncated incorrect DOUBLE value: 'Penguin Books'
答案 0 :(得分:1)
删除您的表(或数据库),然后再次运行syncdb
。您应该在模型中进行更改后执行此操作。
错误是因为您的数据库将列Publisher
存储为DOUBLE
,并且您尝试在其中插入字符串Penguin Books
。
您已经对模型进行了更改,并且没有删除并重新创建表(通过运行syncdb
),或者您正在执行以下操作:
book = Book()
book.title = 'Some Title'
book.publisher = 'Penguin Books'
book.save()
以上问题是publisher
是外键,因此您需要添加对Publisher
模型的引用,如下所示:
publisher = Publisher()
publisher.name = 'Penguin Books'
publisher.save()
book = Book()
book.title = 'Some Title'
book.publisher = publisher # here you are assigning the "Publisher" object
book.save()
答案 1 :(得分:0)
问题很简单,因为您没有在fields
参数中注册这些名称。
这应该是这样的:
class BookAdmin(admin.ModelAdmin):
fields = ('title', 'publisher')
list_display = ('title', 'publisher')
admin.site.register(Book, BookAdmin)