这有两个问题。我正在调查一群学生。我有调查对象,学生对象和脆弱的ModelForms。
在我脆弱的ModelForm中,我正在使用self.helper.layout
,我在那里有一个模板标签......
self.helper.layout = Layout(
'surveyset', # won't display this eventually
'student', # this will also be hidden
# ...
HTML("""
<p>Complete this scale using your knowledge of <b>{{form.student}}</b> during this school year.</p>
""")
现在如何设置我的视图
{{form.student))
在模板中呈现。这就是我到目前为止......我需要使用FormSet
但是当我们到达那里时我们将穿过那座桥......
def select_and_rate(request):
classrooms = Classroom.objects.filter(user=request.user)
return render(request, "reports/select-students.html", {'classrooms':classrooms})
@login_required
def screen_many(request):
# All right we're gonna have a set of surveys!
ss = SurveySet()
ss.save()
# Sorting...
i1 = sorted([(k,v) for k,v in request.POST.items() if k[:2] == 'i_'], key=itemgetter(1))
e1 = sorted([(k,v) for k,v in request.POST.items() if k[:2] == 'e_'], key=itemgetter(1))
i = [int(v) for k,v in request.POST.items() if k[:2] == 'i_']
e = [int(v) for k,v in request.POST.items() if k[:2] == 'e_']
i_students = Student.objects.filter(pk__in=i)
e_students = Student.objects.filter(pk__in=e)
# make a formset with a bunch of surveys with surveyset FK and student FK filled out
# prefilled_survey = Survey(student=stu, surveyset=ss)
# sform = SurveyForm(request.POST or None, instance=prefilled_survey)
# SurveyFormSet = formset_factory(sform,form=SurveyForm, extra=len(e)+len(i))
# some how fit form to intern/extern attribute (minus parts)
# make a new surveyset
# make lists of tuples sorted by key
survey = Survey(surveyset=ss, student=i_students[0])
form = SurveyForm(request.POST or None, )
if form.is_valid():
saved = form.save(commit=False)
saved.surveyset = ss
saved.student = i_students[0]
saved.save()
messages.success(request, "submitted.")
return render_to_response("reports/screen_many.html",
locals(),
context_instance=RequestContext(request))
谢谢!