0

当我尝试list_display在我的 djangoadmin.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'
4

2 回答 2

1

删除您的表(或数据库),然后syncdb再次运行。您应该在对模型进行更改后执行此操作。

该错误是因为您的数据库将该列存储Publisher为 aDOUBLE并且您试图在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()
于 2013-07-14T13:41:49.757 回答
0

问题很简单,因为您没有在fields参数中注册这些名称。

它应该是这样的:

class BookAdmin(admin.ModelAdmin):
    fields = ('title', 'publisher')
    list_display = ('title', 'publisher')

admin.site.register(Book, BookAdmin)
于 2013-07-14T13:35:26.607 回答