gpt4 book ai didi

python - Django 管理员 TypeError : buffer object expected

转载 作者:太空宇宙 更新时间:2023-11-04 10:13:04 25 4
gpt4 key购买 nike

在 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

最佳答案

BinaryField仅用于原始二进制数据的引用 - 它不是 1 或 0 字段。

相反,您应该使用 BooleanField

    available = models.BooleanField(default=True)

或者只是IntegerField

    available = models.IntegerField(default=1)

请注意,如果您不为 BooleanField 设置 default,它就不会是 False,而是 None!

关于python - Django 管理员 TypeError : buffer object expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37067039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com