5

当我尝试保存(使用 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
4

2 回答 2

9

是的,这将是一个公平的假设。正如您从源代码中看到的那样,该方法的签名是def get_db_prep_value(self, value, connection, prepared=False),因此任何子类都需要期望相同的参数或采用*args, **kwargs

于 2013-01-03T12:11:39.620 回答
8

丹尼尔·罗斯曼(Daniel Roseman)几乎回答了这个问题,但我会用另一种方式陈述它并使用更多的词,希望你能理解。对于 Django 1.4.x 中的自定义字段,您的自定义字段的 get_db_prep_value 函数现在必须接受4 个参数。在 Django 1.2 中,Django 开发人员引入了多数据库支持。在 Django 1.4.x 中,开发人员删除了允许您的函数声明工作的代码。

那么你需要做什么?您需要重新定义 HibernateBooleanField.get_db_prep_value 以接受额外的参数。像下面这样更改您的 get_db_prep_value 函数,它将起作用:)

def get_db_prep_value(self, value, connection, prepared=False):
    return 0x01 if value else 0x00

Django 文档参考:https ://docs.djangoproject.com/en/1.2/howto/custom-model-fields/#django.db.models.Field.get_db_prep_value

于 2013-02-08T06:12:37.873 回答