Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid ProgrammingError: can't adapt type 'DateTimeRangeField' when saving a Django model instance to a remote database?

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:

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 ReportPeriod.objects.using('remote_db').filter(id='1',active_range__contains=datetime.now()) returns the expected 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:

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?

like image 675
JAC Avatar asked Jul 05 '15 02:07

JAC


1 Answers

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:

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.

like image 176
Adam Johnson Avatar answered Nov 14 '22 22:11

Adam Johnson