python - Django 管理员保存错误 : get_db_prep_value() got an unexpected keyword argument 'connection'

标签 python django django-admin

当我尝试保存(使用 Django 标准管理界面)时,出现以下错误...

TypeError at /admin/web/campaign/dc6eb21f-87fa-462f-88af-416cf6be37f6/

get_db_prep_value() got an unexpected keyword argument 'connection'

有人可以向我解释一下,为什么以及可能的解决方案吗?我假设这与我的自定义 HibernateBooleanField 字段有关。

下面是完整的错误和详细信息。

错误

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/admin/web/campaign/dc6eb21f-87fa-462f-88af-416cf6be37f6/

Django Version: 1.4.2
Python Version: 2.7.2
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.flatpages',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'grappelli.dashboard',
 'grappelli',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'web')
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 "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in wrapper
  366.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  89.         response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py" in inner
  196.             return view(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapper
  25.             return bound_func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in bound_func
  21.                 return func(self, *args2, **kwargs2)
File "/Library/Python/2.7/site-packages/django/db/transaction.py" in inner
  209.                 return func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in change_view
  1054.                 self.save_model(request, new_object, form, True)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in save_model
  709.         obj.save()
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in save
  463.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in save_base
  529.                             rows = manager.using(using).filter(pk=pk_val)._update(values)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in _update
  560.         return query.get_compiler(self.db).execute_sql(None)
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  986.         cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  808.             sql, params = self.as_sql()
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in as_sql
  951.                 val = field.get_db_prep_save(val, connection=self.connection)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in get_db_prep_save
  292.                                       prepared=False)

Exception Type: TypeError at /admin/web/campaign/dc6eb21f-87fa-462f-88af-416cf6be37f6/
Exception Value: get_db_prep_value() got an unexpected keyword argument 'connection'

型号

class Campaign(models.Model):
    campaignid = models.CharField(max_length=255, primary_key=True, db_column='campaignID')
    name = models.CharField(max_length=105)
    active = HibernateBooleanField(default=False)
    created = models.DateTimeField()
    modified = models.DateTimeField(null=True, blank=True)
    companyid = models.ForeignKey(Company, null=True, db_column='companyID', blank=True)

    class Meta:
        db_table = u'campaign'


    def __unicode__(self):
        return self.name

class HibernateBooleanField(models.BooleanField):

    __metaclass__ = models.SubfieldBase

    def get_internal_type(self):
        return "HibernateBooleanField"

    def db_type(self):
        return 'bit(1)'

    def to_python(self, value):
        if value in (True, False): return value
        if value in ('t', 'True', '1', '\x01'): return True
        if value in ('f', 'False', '0', '\x00'): return False

    def get_db_prep_value(self, value):
        return 0x01 if value else 0x00

最佳答案

是的,这是一个合理的假设。从the source code可以看出,该方法的签名是 def get_db_prep_value(self, value, connection, prepared=False),因此任何子类都需要期望相同的参数或采用 *args, **kwargs.

关于python - Django 管理员保存错误 : get_db_prep_value() got an unexpected keyword argument 'connection' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14138586/

相关文章:

javascript - Django - 在模板中显示 Json 或 Httpresponse

python - 使用网络爬虫访问链接时遇到问题

python - 在 groupby 之后填充组中缺失的行

python - 无法更新在 Google Cloud Platform 上构建的网站

python - 如何从 Pandas 中的df中随机删除10%的属性值

python - 使用注释代替模型属性

jquery - 提交表单而不刷新/重定向当前页面(Python(Django)+ jQuery + Ajax)

javascript - 如何覆盖 django 管理模板以添加自定义 javascript?

python - 彻底使用管理员界面

python - 在 Django 管理屏幕中删除 "add another"