django“__ init__至少有两个参数”用于动态下拉列表功能 [英] django "__init__takes at least 2 arguments" for dynamic drop down list function

查看:151
本文介绍了django“__ init__至少有两个参数”用于动态下拉列表功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于django动态下拉列表,我做了一些试验,

Regarding the django dynamic drop down list, I made some trials,

如果没有动态功能,它可以没有问题

from django import forms
from django.forms import ModelForm
from .models import input
from result.models import result
from django.contrib.auth.models import User,Group
import Queue

class inputform(forms.ModelForm):

    regionlist = forms.ModelChoiceField(queryset=result.objects.values('Region').distinct())

class Meta:
    model = input
    fields = ('company', 'Region')

如果添加下面的动态函数, __Init__至少有2个参数(2个)

...before are same as above....
class inputform(forms.ModelForm):
    region = forms.ModelChoiceField(label=u'Region')


    def __init__(self,*args,**kwargs):
        super(inputform,self).__init__(*args,**kwargs)
        self.fields['region'].choices=((x.que,x.disr) for x in result.objects.values('Region').distinct())

..... 。以下与上述相同...

......below are the same as above one...

追溯

Traceback:
File "C:\Python27\lib\site-packages\django-1.8.3 py2.7.egg\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\decorators\csrf.py" in wrapped_view
58.         return view_func(*args, **kwargs)
File "C:\Users\user\XXX\inputform\views.py" in input
13.         form = inputform() #??????
File "C:\Users\user\Desktop\XXX\inputform\forms.py" in __init__
22.         self.fields['region'].choices=((x.que,x.disr) for x in result.objects.values('Region').distinct())
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\forms\fields.py" in _set_choices
851.             value = list(value)
File "C:\Users\user\Desktop\XXX\inputform\forms.py" in <genexpr>
22.         self.fields['region'].choices=((x.que,x.disr) for x in result.objects.values('Region').distinct())

Exception Type: AttributeError at /input
Exception Value: 'dict' object has no attribute 'que'

第二次尝试

region = forms.ModelChoiceField(queryset=None,label=u'region')

def __init__(self,*args,**kwargs):
    super(InputForm,self).__init__(*args,**kwargs)
    iquery = Result.objects.values_list('region', flat=True).distinct()
    iquery_choices = [('', '')] + [(region,region)  for region in iquery]

我认为现在是dic,但它仍然报告相同的错误。请提前感谢。

I think now it is dic, but it still reports the same error. Please help thanks in advance.

推荐答案

您必须将查询集传递给您的 ModelChoiceField 。从文档:

You must pass queryset to your ModelChoiceField. From docs:


对于更复杂的用途,您可以指定 queryset = None 声明表单字段,然后在窗体的 __ init __()方法中填充查询集:

For more complex uses, you can specify queryset=None when declaring the form field and then populate the queryset in the form’s __init__() method:




$ b region = forms.ModelChoiceField(queryset = None,label = u'Region')

$

class inputform(forms.ModelForm):
    region = forms.ModelChoiceField(queryset=None, label=u'Region')


    def __init__(self,*args,**kwargs):
        super(inputform,self).__init__(*args,**kwargs)
        self.fields['region'].choices=((x['Region'].que,x['Region'].disr) for x in dupont.objects.values('Region').distinct())

这篇关于django“__ init__至少有两个参数”用于动态下拉列表功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆