2013-06-12 80 views
0
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的計數因我的應用程序而異,因此難以將它們添加到選項中並使用複選框小部件。需要複選框使用Django形式

回答

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)