1

我很难解决这个问题,如果您知道这一点,请提供帮助。我在 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

意见.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')
4

1 回答 1

1

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

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

通过django-mysql包还支持 MySQL 。

于 2018-07-11T00:13:13.687 回答