需要使用Django表单的列表复选框

时间:2013-06-12 10:24:25

标签: django-forms django-templates django-views

forms.py

from django import forms
from .models import VendorApplication, VendorAppWorkFlow
import MySQLdb as mdb
from django.forms.fields import DateField, ChoiceField, MultipleChoiceField
from django.forms.widgets import RadioSelect, CheckboxSelectMultiple
from django.forms.extras.widgets import SelectDateWidget


con = mdb.connect('', '', '', '');
cursor1 = con.cursor()
cursor1.execute("select * from vendorapp_sharetable;")
columns = cursor1.description
num_fields = len(cursor1.description)
field_names = [i[0] for i in cursor1.description]

class CheckForm(forms.Form):
    for index in range(num_fields):
       field_names[index] = forms.BooleanField(required=False, label=field_names[index])

我想将forms.py中的field_names值添加到复选框列表中而没有选择,或者我如何在选择中迭代它们以获得checkboxMultiselect widget。由于field_names的计数在我的应用程序中有所不同,因此很难将它们添加到选项中并使用复选框小部件。

1 个答案:

答案 0 :(得分:0)

类似的东西:

field_names = [i[0] for i in cursor1.description]

choices = [(x, y) for (x, y) in enumerate (field_names)]

class CheckForm (forms.Form):
    fields = forms.CheckboxMultiSelect (choices = choices, required = False)