Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

InterfaceError (binding parameter) while creating a model

I'd like my users to subscribe on car brands and follow related reviews and/or classified ads.

I extended django's User model with:

class Subscriptions(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    cars = JSONField(default=list)

My view function:

def make_subscribe(request, make):
    subs, created = Subscriptions.objects.get_or_create(user=request.user)
    subs.cars.append({'make': make, 'reviews': True, 'listings': True})
    return redirect("reviews:model-select", make=make)

Traceback of the error I get while attempting to subscribe:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/reviews/BMW/subscribe/

Django Version: 1.11.1
Python Version: 3.6.1
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'import_export',
 'vote',
 'django_countries',
 'reviews.apps.ReviewsConfig',
 'home.apps.HomeConfig',
 'marketplace.apps.MarketplaceConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\models\query.py" in get_or_create
  464.             return self.get(**lookup), False

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\models\query.py" in get
  380.                 self.model._meta.object_name

During handling of the above exception (Subscriptions matching query does not exist.), another exception occurred:

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\backends\utils.py" in execute
  65.                 return self.cursor.execute(sql, params)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\backends\sqlite3\base.py" in execute
  328.         return Database.Cursor.execute(self, query, params)

The above exception (Error binding parameter 1 - probably unsupported type.) was the direct cause of the following exception:

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\backends\utils.py" in execute
  80.             return super(CursorDebugWrapper, self).execute(sql, params)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\backends\utils.py" in execute
  65.                 return self.cursor.execute(sql, params)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\utils.py" in __exit__
  94.                 six.reraise(dj_exc_type, dj_exc_value, traceback)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\utils\six.py" in reraise
  685.             raise value.with_traceback(tb)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\backends\utils.py" in execute
  65.                 return self.cursor.execute(sql, params)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\backends\sqlite3\base.py" in execute
  328.         return Database.Cursor.execute(self, query, params)

During handling of the above exception (Error binding parameter 1 - probably unsupported type.), another exception occurred:

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\core\handlers\exception.py" in inner
  41.             response = get_response(request)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\And\Desktop\steerr\reviews\views.py" in make_subscribe
  177.  subs = Subscriptions.objects.get_or_create(user=request.user)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\models\manager.py" in manager_method
  85.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\models\query.py" in get_or_create
  466.             return self._create_object_from_params(lookup, params)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\models\query.py" in _create_object_from_params
  498.                 obj = self.create(**params)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\models\query.py" in create
  394.         obj.save(force_insert=True, using=self.db)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\models\base.py" in save
  806.                        force_update=force_update, update_fields=update_fields)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\models\base.py" in save_base
  836.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\models\base.py" in _save_table
  922.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\models\base.py" in _do_insert
  961.                                using=using, raw=raw)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\models\manager.py" in manager_method
  85.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\models\query.py" in _insert
  1061.         return query.get_compiler(using=using).execute_sql(return_id)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\models\sql\compiler.py" in execute_sql
  1099.                 cursor.execute(sql, params)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\backends\utils.py" in execute
  84.             sql = self.db.ops.last_executed_query(self.cursor, sql, params)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\backends\sqlite3\operations.py" in last_executed_query
  135.                 params = self._quote_params_for_last_executed_query(params)

File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.1-py3.6.egg\django\db\backends\sqlite3\operations.py" in _quote_params_for_last_executed_query
  124.             return cursor.execute(sql, params).fetchone()

Exception Type: InterfaceError at /reviews/BMW/subscribe/
Exception Value: Error binding parameter 1 - probably unsupported type.

Any suggestion or feedback will be welcomed and greatly appreciated.

Thank you.

2 Answers

Hi Andetin

Have you imported the JSONfield? if not that could be causing the issue.

from django.contrib.postgres.fields import JSONField

Hello Andreas,

Yes I did. There is no problem if I switch to CharField instead of JSONField, but I need to save data containing dictionaries and booleans. Such as:

[
    {make: 'BMW', reviews: True, listings: True},
    {make: 'Audi', reviews: True, listings: False}
]

i will have a look at the later and get back to you.