将对象保存到数据库时发生问题,因为模型包含jsonfield sqlite3 django

时间:2018-07-10 23:14:54

标签: python django django-jsonfield

我真的很难解决此问题,如果您知道的话,请提供帮助。我在django上有一个项目,它包含具有两个字段的模型,一个是外键类型,另一个是jsonfield。当将对象保存到数据库中时,它不会被保存而是抛出异常。我不知道是什么问题。但是问题仅特定于sqlite3数据库。它可以与其他数据库正常工作。我正在粘贴堆栈跟踪和代码。它会在此行上引发异常student_record_obj.save()

Internal Server Error: /tableapp/postdata/
Traceback (most recent call last):
  File "C:\Python35\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Python35\lib\site-packages\django\db\backends\sqlite3\base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.InterfaceError: Error binding parameter 1 - probably unsupported type.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Python35\lib\site-packages\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "C:\Python35\lib\site-packages\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Python35\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Python35\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Python35\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Python35\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Python35\lib\site-packages\django\db\backends\sqlite3\base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.InterfaceError: Error binding parameter 1 - probably unsupported type.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python35\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Python35\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Python35\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Python35\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "C:\Users\Jdidi\Documents\Fresh\calendardjangoapp\tableapp\views.py", line 89, in posting_data
    student_record_obj.save()
  File "C:\Python35\lib\site-packages\django\db\models\base.py", line 729, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Python35\lib\site-packages\django\db\models\base.py", line 759, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\Python35\lib\site-packages\django\db\models\base.py", line 842, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "C:\Python35\lib\site-packages\django\db\models\base.py", line 880, in _do_insert
    using=using, raw=raw)
  File "C:\Python35\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Python35\lib\site-packages\django\db\models\query.py", line 1125, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Python35\lib\site-packages\django\db\models\sql\compiler.py", line 1280, in execute_sql
    cursor.execute(sql, params)
  File "C:\Python35\lib\site-packages\django\db\backends\utils.py", line 104, in execute
    sql = self.db.ops.last_executed_query(self.cursor, sql, params)
  File "C:\Python35\lib\site-packages\django\db\backends\sqlite3\operations.py", line 136, in last_executed_query
    params = self._quote_params_for_last_executed_query(params)
  File "C:\Python35\lib\site-packages\django\db\backends\sqlite3\operations.py", line 125, in _quote_params_for_last_executed_query
    return cursor.execute(sql, params).fetchone()
sqlite3.InterfaceError: Error binding parameter 1 - probably unsupported type.
[10/Jul/2018 23:01:32] "POST /tableapp/postdata/ HTTP/1.1" 500 18778

VIEWS.PY:

 def posting_data(request):
    if request.method == "POST":
        response_ls = json.loads(request.POST.get('data', []))
        final_ls = []
        for dic in response_ls:
            if dic and "csrfmiddlewaretoken" not in dic:
                final_ls.append(dic)

        try:
            student_record_obj = StudentRecords.objects.get(user=request.user)
            student_record_obj.data = final_ls
        except StudentRecords.DoesNotExist:
            attrs = {'user':request.user , 'data': final_ls}
            student_record_obj = StudentRecords(**attrs)

        student_record_obj.save()
    return redirect('/tableapp/index')

1 个答案:

答案 0 :(得分:2)

JSONField是PostgreSQL特定字段。不能与SQLite一起使用:

https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/fields/#jsonfield

还通过django-mysql软件包支持MySQL。