如何避免ProgrammingError:在将Django模型实例保存到远程数据库时,无法调整类型'DateTimeRangeField'?

时间:2015-07-05 02:09:16

标签: python django postgresql range

我有一个包含DateTimeRangeField的模型,如https://docs.djangoproject.com/en/1.8/ref/contrib/postgres/fields/#defining-your-own-range-types中所述,见下文:

models.py

from django.db import models
from django.contrib.postgres.fields import DateTimeRangeField, RangeField

class ReportPeriod(models.Model):
  id = models.IntegerField(primary_key=True)
  period_name = models.TextField(blank=True)
  active_range = DateTimeRangeField(blank=True) 
  class Meta:
    managed = False
    db_table = 'report_period'

当我使用它来查询远程数据库时,模型工作正常(例如ReportPeriod.objects.using('remote_db').filter(id='1',active_range__contains=datetime.now())返回预期的QuerySet)。

但是,当我尝试在我的视图或shell中保存新的ReportPeriod时,我得到ProgrammingError: can't adapt type 'DateTimeRangeField'。以下是我在收到错误之前在shell中执行的步骤:

new_period = ReportPeriod(id=1,period_name = 'morning',active_range = DateTimeRangeField(datetime(2015,1,1,0,0,0),datetime(2016,1,1,0,0,0)))
new_period.save(using='remote_db')

这是整个错误跟踪:

Traceback (most recent call last):
 File "<console>", line 1, in <module>
 File "~/.virtualenvs/mve/lib/python2.7/site-packages/django/db/models/base.py", line 710, in save
force_update=force_update, update_fields=update_fields)
 File "~/.virtualenvs/mve/lib/python2.7/site-packages/django/db/models/base.py", line 738, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
 File "~/.virtualenvs/mve/lib/python2.7/site-packages/django/db/models/base.py", line 803, in _save_table
forced_update)
 File "~/.virtualenvs/mve/lib/python2.7/site-packages/django/db/models/base.py", line 853, in _do_update
return filtered._update(values) > 0
 File "~/.virtualenvs/mve/lib/python2.7/site-packages/django/db/models/query.py", line 580, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
 File "~/.virtualenvs/mve/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1062, in execute_sql
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
 File "~/.virtualenvs/mve/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 840, in execute_sql
cursor.execute(sql, params)
 File "~/.virtualenvs/mve/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
 File "~/.virtualenvs/mve/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
 File "~/.virtualenvs/mve/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
 File "~/.virtualenvs/mve/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
ProgrammingError: can't adapt type 'DateTimeRangeField'

我在模型定义中遗漏了什么吗?有人知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:5)

DateTimeRangeField是字段类,只应用于模型定义。要创建具有日期范围的对象,您应该使用DateTimeRange中的psycopg2.extras类:

from psycopg2.extras import DateTimeRange

new_period = ReportPeriod(
    id=1,
    period_name='morning',
    active_range=DateTimeRange(datetime(2015, 1, 1, 0, 0, 0), datetime(2016, 1, 1, 0, 0, 0))
)
new_period.save(using='remote_db')

django.contrib.postgres文档中没有详细记录,只显示NumericRange,但我发现此用法示例in the DateTimeRangeField tests