Django表单 - 错误绑定参数0 - 可能不受支持的类型

时间:2016-02-05 09:46:22

标签: django django-models django-forms django-views

当我尝试发送表单时,我收到的错误如“错误绑定参数0 - 可能不支持的类型。”。

我检查了我的模型并为所有测试字段设置了blank = True但没有帮助。你能给我一些建议吗?或者新鲜的样子:)

我的追溯:

Environment:

Request Method: POST
Request URL: http://127.0.0.1:8000/panel/order/create/19/

Django Version: 1.8.8
Python Version: 3.5.1
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'core',
 'client',
 'registration',
 'avatar',
 'filer',
 'mptt',
 'easy_thumbnails',
 'reversion',
 'guardian')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "C:\Users\loc\PycharmProjects\CRM\core\views.py" in order_create
  347.         form_order = OrderCreateForm(request.POST)
File "C:\Users\loc\PycharmProjects\CRM\core\forms.py" in __init__
  34.             Platform.objects.filter(client_id=request_client_id).values_list('id', 'name'))
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\query.py" in __iter__
  162.         self._fetch_all()
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\query.py" in _fetch_all
  965.             self._result_cache = list(self.iterator())
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\query.py" in iterator
  1220.             for row in compiler.results_iter():
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\sql\compiler.py" in results_iter
  794.             results = self.execute_sql(MULTI)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  840.             cursor.execute(sql, params)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\backends\utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\backends\utils.py" in execute
  64.                 return self.cursor.execute(sql, params)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\utils.py" in __exit__
  98.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\utils\six.py" in reraise
  685.             raise value.with_traceback(tb)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\backends\utils.py" in execute
  64.                 return self.cursor.execute(sql, params)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\backends\sqlite3\base.py" in execute
  318.         return Database.Cursor.execute(self, query, params)

Exception Type: InterfaceError at /panel/order/create/19/
Exception Value: Error binding parameter 0 - probably unsupported type.

Forms.py:

BLANK_CHOICE = (('', '---------'),)

class OrderCreateForm(forms.ModelForm):
    tag_from = forms.MultipleChoiceField(label='Tags')
    tag_to = forms.MultipleChoiceField()

    class Meta:
        model = Order
        fields = ('price', 'client', 'platform')

    def __init__(self, request_client_id, *args, **kwargs):
        super(OrderCreateForm, self).__init__(*args, **kwargs)
        self.fields['platform'].choices = BLANK_CHOICE + tuple(
            Platform.objects.filter(client_id=request_client_id).values_list('id', 'name'))
        self.fields['tag_from'].choices = OrderItemList.objects.all().values_list('id', 'name')

View.py:

 @user_passes_test(lambda u: u.is_staff, login_url='/account/login/')
def order_create(request, request_client_id):
    dict = {}

    if request.method == 'POST':
        form_order = OrderCreateForm(request.POST)
       if form_order.is_valid():
            obj = form_order.save(commit=False)
            (...)
            obj.save()
            return HttpResponse('..')

        dict['form_order'] = form_order
        return render(request, 'panel/order/form.html', dict)
    else:
        dict['form_order'] = OrderCreateForm(request_client_id)
        return render(request, 'panel/order/form.html', dict)

2 个答案:

答案 0 :(得分:2)

您的表单将request_client_id作为第一个参数。因此,您必须在提交表单的每个时间内传递request_client_id ,包括在您发布帖子请求时。

if request.method == 'POST':
    form_order = OrderCreateForm(request_client_id, request.POST)

答案 1 :(得分:0)

改变你的形式:

BLANK_CHOICE = ((0, '---------'),)

class OrderCreateForm(forms.ModelForm):
    tag_from = forms.MultipleChoiceField(label='Tags')
    tag_to = forms.MultipleChoiceField()

    class Meta:
        model = Order
        fields = ('price', 'client', 'platform')

    def __init__(self, request_client_id, *args, **kwargs):
        super(OrderCreateForm, self).__init__(*args, **kwargs)
        self.fields['platform'].choices = BLANK_CHOICE + tuple((x.id, x.name) for x in Platform.objects.filter(client_id=request_client_id))