postgresql 将 Django 模型实例保存到远程数据库时,如何避免 ProgrammingError: can't adapt type 'DateTimeRangeField'?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31226625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I avoid ProgrammingError: can't adapt type 'DateTimeRangeField' when saving a Django model instance to a remote database?
提问by JAC
I have a model that includes a DateTimeRangeField, as described in https://docs.djangoproject.com/en/1.8/ref/contrib/postgres/fields/#defining-your-own-range-types, see below:
我有一个包含 DateTimeRangeField 的模型,如https://docs.djangoproject.com/en/1.8/ref/contrib/postgres/fields/#defining-your-own-range-types 中所述,见下文:
models.py
模型.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'
The model works fine when I use it to query a remote database (for example ReportPeriod.objects.using('remote_db').filter(id='1',active_range__contains=datetime.now())
returns the expected QuerySet).
当我使用它来查询远程数据库(例如ReportPeriod.objects.using('remote_db').filter(id='1',active_range__contains=datetime.now())
返回预期的 QuerySet)时,该模型工作正常。
However when I try to save a new ReportPeriod in my views or in the shell I get a ProgrammingError: can't adapt type 'DateTimeRangeField'
. Here are the steps that I follow in the shell before getting the error:
但是,当我尝试在我的视图或 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')
And this is the entire error trace:
这是整个错误跟踪:
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'
Am I missing something in the model definition? Does anybody know how to solve this issue?
我在模型定义中遗漏了什么吗?有谁知道如何解决这个问题?
回答by Adam Johnson
DateTimeRangeField
is the field class and should only be used for the model definition. To create objects with a date range, you should use the DateTimeRange
class from psycopg2.extras
:
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')
It's not well documented in the django.contrib.postgres docs which only show you NumericRange
, but I found this usage example in the DateTimeRangeField tests.
它在 django.contrib.postgres 文档中没有很好地记录,它只向你展示NumericRange
,但我在 DateTimeRangeField 测试中找到了这个用法示例。