Django admin TypeError:期望缓冲对象

时间:2016-05-06 07:45:53

标签: python django django-admin

在Django管理页面中为模型添加数据时,我收到一个错误,但是对于导致错误的原因并没有很好的描述。

将数据直接添加到数据库中就可以了......

导致此错误的原因是什么?

型号:

class Partner(models.Model):
    name = models.CharField(max_length=70)
    link = models.CharField(max_length=125)
    available = models.BinaryField(default=1)
    price = models.IntegerField(default=0)

    def __str__(self):
        return self.name

错误:

Request Method: POST
Request URL: http://localhost:8000/admin/myapp/partner/add/

Django Version: 1.9.5
Python Version: 2.7.11
Installed Applications:
['myapp.apps.MyappConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in wrapper
  541.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py" in inner
  244.             return view(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in add_view
  1437.         return self.changeform_view(request, None, form_url, extra_context)

File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapper
  67.             return bound_func(*args, **kwargs)

File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\django\utils\decorators.py" in bound_func
  63.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "C:\Python27\lib\site-packages\django\utils\decorators.py" in inner
  184.                     return func(*args, **kwargs)

File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in changeform_view
  1378.                 self.save_model(request, new_object, form, not add)

File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in save_model
  991.         obj.save()

File "C:\Python27\lib\site-packages\django\db\models\base.py" in save
  708.                        force_update=force_update, update_fields=update_fields)

File "C:\Python27\lib\site-packages\django\db\models\base.py" in save_base
  736.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "C:\Python27\lib\site-packages\django\db\models\base.py" in _save_table
  820.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

File "C:\Python27\lib\site-packages\django\db\models\base.py" in _do_insert
  859.                                using=using, raw=raw)

File "C:\Python27\lib\site-packages\django\db\models\manager.py" in manager_method
  122.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "C:\Python27\lib\site-packages\django\db\models\query.py" in _insert
  1039.         return query.get_compiler(using=using).execute_sql(return_id)

File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  1059.             for sql, params in self.as_sql():

File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in as_sql
  1019.                 for obj in self.query.objs

File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in prepare_value
  958.             value = field.get_db_prep_save(value, connection=self.connection)

File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_save
  728.                                       prepared=False)

File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_value
  2353.             return connection.Database.Binary(value)

Exception Type: TypeError at /admin/myapp/partner/add/
Exception Value: buffer object expected

2 个答案:

答案 0 :(得分:3)

BinaryField仅供参考原始二进制数据 - 它不是1 or 0字段。

相反,您应该使用BooleanField

    available = models.BooleanField(default=True)

或只是IntegerField

    available = models.IntegerField(default=1)

请注意,如果您未能为BooleanField设置default,则它不会False而是None

答案 1 :(得分:1)

BinaryField用于存储二进制数据,即原始字节。 buffer object是一个公开接口的对象,允许Python访问其原始字节,并且您为可用字段提供的默认值不是这样的对象。这就是Django抱怨的原因。

正如Muhammad Tahir在评论中所建议的那样,你很可能想要使用BooleanField