How can I avoid ProgrammingError: Cannot adapt DateTimeRangeField type when saving Django model instance to remote database?
I have a model that includes DateTimeRangeField as described at https://docs.djangoproject.com/en/1.8/ref/contrib/postgres/fields/#defining-your-own-range-types , see below:
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'
The model works fine when I use it to query a remote database (for example, it ReportPeriod.objects.using('remote_db').filter(id='1',active_range__contains=datetime.now())
returns the expected QuerySet).
However, when I try to store the new ReportPeriod in my views or in the shell, I get ProgrammingError: can't adapt type 'DateTimeRangeField'
. Below are the steps I am following in the shell before getting the error:
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 whole error tracing:
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 anyone know how to fix this problem?
source to share
DateTimeRangeField
is the field class and should only be used to define the model. To create objects with a range of dates, you must use the class DateTimeRange
from 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 is not well documented in the django.contrib.postgres docs which only show NumericRange
, but I found this usage example in DateTimeRangeField tests .
source to share