简体   繁体   中英

Django admin TypeError: buffer object expected

When adding data for a model in the Django admin page I get an error, but it's not very descriptive on what is causing the error.

Adding the data directly to the database works just fine...

What is causing this error?

The model:

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

The error:

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

The BinaryField is due to reference only for raw, binary data - it is not 1 or 0 field.

Instead you should use BooleanField

    available = models.BooleanField(default=True)

or just IntegerField

    available = models.IntegerField(default=1)

Notice that if you won't set default for BooleanField it won't be False but None !

BinaryField is used to stored binary data, that is raw bytes. A buffer object is an object that exposes a interface to allow Python to access its raw bytes, and the default value that you give to the field available is not such an object. That is why Django complains.

As suggested by Muhammad Tahir in the comments, mostly likely you wanted to use a BooleanField .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM