用type动态创建Form
Posted TAMAYURA
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用type动态创建Form相关的知识,希望对你有一定的参考价值。
type时所有元类的父亲
object是type(object的类型是type),type也是object(type继承自object) >>> isinstance(object, type) True >>> isinstance(type, object) True 在python中一切皆object,所有对象都是从object继承而来的,type也是object,而object的type是type >>> type.__base__ <type ‘object‘> >>> type(object) <type ‘type‘> 1、type是一切type的类型 2、object是所有对象的基类,type也继承自object 3、object的type是type 去掉object所有对象都不能用了,而去掉type,所有对象还是可以正常运作的,只是没有一个东西标识他的类型了
def score(request, class_id, qn_id): """ :param request: :param class_id: 班级ID :param qn_id: 问卷ID :return: """ student_id = request.session[‘student_info‘][‘id‘] # 1. 当前登录用户是否是要评论的班级的学生 ct1 = models.Student.objects.filter(id=student_id, cls_id=class_id).count() if not ct1: return HttpResponse(‘你只能评论自己班级的问卷,是不是想转班?‘) # 2. 你是否已经提交过当前问卷答案 ct2 = models.Answer.objects.filter(stu_id=student_id, question__naire_id=qn_id).count() if ct2: return HttpResponse(‘你已经参与过调查,无法再次进行‘) # 3. 展示当前问卷下的所有问题 # question_list = models.Question.objects.filter(naire_id=qn_id) from django.forms import Form from django.forms import fields from django.forms import widgets # # 类:方式一 # class TestForm(Form): # tp1 = fields.ChoiceField(label=‘路宁傻不傻?‘,choices=[ (i,i) for i in range(1,11)],widget=widgets.Radioselect) # tp2 = fields.ChoiceField(label=‘路宁傻不傻?‘,choices=[ (i,i) for i in range(1,11)],widget=widgets.RadioSelect) # tp3 = fields.CharField(label=‘对路宁的建议?‘,widget=widgets.Textarea) # tp4 = fields.ChoiceField(label=‘路宁帽子颜色?‘,choices=[ (i,i) for i in range(1,11)],widget=widgets.RadioSelect) # # # 类:方式二 # MyTestForm = type("MyTestForm",(Form,),{ # ‘tp1‘: fields.ChoiceField(label=‘路宁傻不傻?‘,choices=[ (i,i) for i in range(1,11)],widget=widgets.RadioSelect), # ‘tp2‘: fields.ChoiceField(label=‘路宁傻不傻?‘,choices=[ (i,i) for i in range(1,11)],widget=widgets.RadioSelect), # ‘tp3‘: fields.CharField(label=‘对路宁的建议?‘,widget=widgets.Textarea), # ‘tp4‘: fields.ChoiceField(label=‘路宁帽子颜色?‘,choices=[ (i,i) for i in range(1,11)],widget=widgets.RadioSelect), # }) # return render(request,‘score.html‘,{‘question_list‘:question_list,‘form‘:MyTestForm()}) question_list = models.Question.objects.filter(naire_id=qn_id) field_dict = {} for que in question_list: if que.tp == 1: field_dict[‘val_%s‘ % que.id] = fields.ChoiceField( label=que.caption, error_messages={‘required‘:‘必填‘}, widget=widgets.RadioSelect, choices=[(i, i) for i in range(1, 11)] ) elif que.tp == 2: field_dict[‘option_id_%s‘ % que.id] = fields.ChoiceField( label=que.caption, widget=widgets.RadioSelect, choices=models.Option.objects.filter( qs_id=que.id).values_list(‘id‘, ‘name‘)) else: from django.core.exceptions import ValidationError from django.core.validators import RegexValidator # field_dict[‘x_%s‘ % que.id] = fields.CharField( # label=que.caption, widget=widgets.Textarea,validators=[RegexValidator(regex=""),]) field_dict[‘content_%s‘ % que.id] = fields.CharField( label=que.caption, widget=widgets.Textarea, validators=[func, ]) # 类:方式二 MyTestForm = type("MyTestForm", (Form,), field_dict) if request.method == ‘GET‘: form = MyTestForm() return render(request, ‘score.html‘, {‘question_list‘: question_list, ‘form‘: form}) else: # 15字验证 # 不允许为空 form = MyTestForm(request.POST) if form.is_valid(): print(form.cleaned_data) # {‘x_2‘: ‘3‘, ‘x_9‘: ‘sdfasdfasdfasdfasdfasdfasdf‘, ‘x_10‘: ‘13‘} objs = [] for key,v in form.cleaned_data.items(): k,qid = key.rsplit(‘_‘,1) answer_dict = {‘stu_id‘:student_id,‘question_id‘:qid,k:v} objs.append(models.Answer(**answer_dict)) models.Answer.objects.bulk_create(objs) return HttpResponse(‘感谢您的参与!!!‘) return render(request, ‘score.html‘, {‘question_list‘: question_list, ‘form‘: form})
以上是关于用type动态创建Form的主要内容,如果未能解决你的问题,请参考以下文章
Python 统一动态创建多个model对应的modelForm类(type()函数)
已解决在react+ts中 atnd 用 upload 组件报错Failed to execute ‘readAsArrayBuffer,param 1 is notof type Blob(代码片段
已解决在react+ts中 atnd 用 upload 组件报错Failed to execute ‘readAsArrayBuffer,param 1 is notof type Blob(代码片段