单选框 复选框

 

#单选方式一

from django.forms import Form
from django.forms import fields
from django.forms import widgets
from app01 import models

class TestForm(Form):
    user = fields.CharField()
    group = fields.CharField(
        choice = []
    )
    
    def __init__(self,*args,**kwargs):
        super(TestForm,self).__init__(*args,**kwargs)
        self.fields['group'].choices=models.UserGroup.objects.values_list('id','title')

#单选方式二
#   依赖model中的__str__

from django.forms import Form
from django.forms import fields
from app01 import models
from django.forms.models import ModelChoiceField

class TestForm(Form):
    user = fields.CharField()
    group = ModelChoiceField(queryset=models.UserGroup.objects.all())



#复选框

from django.forms import Form
from django.forms import fields
from app01 import models
from django.forms.models import ModelMultipleChoiceField

class TestForm(Form):
    user = fields.CharField()
    group = ModelMultipleChoiceField(queryset=models.UserGroup.objects.all())

  

posted @ 2017-09-14 13:52  golangav  阅读(348)  评论(0编辑  收藏  举报