码迷,mamicode.com
首页 > 其他好文 > 详细

单选框 复选框

时间:2017-09-14 14:49:23      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:tip   app   依赖   get   import   复选框   echo   queryset   widget   

 

#单选方式一

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())

  

单选框 复选框

标签:tip   app   依赖   get   import   复选框   echo   queryset   widget   

原文地址:http://www.cnblogs.com/oyoui/p/7520238.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!