Form(或Formset?)来处理Django中的多个表行 [英] Form (or Formset?) to handle multiple table rows in Django
问题描述
起初我以为这是什么formets,但是我看不到任何方式自动迭代数据库表的内容来产生项目在表单中,如果你看到我的意思。
目前,我的views.py有以下代码:
def调查(请求):
ScoreFormSet = formset_factory(ScoreForm)
如果request.method =='POST':
formset = ScoreFormSet(request。 POST,request.FILES)
如果formset.is_valid():
return HttpResponseRedirect('/')
else:
formset = ScoreFormSet()
return render_to_response 'cf / survey.html',{
'formset':formset,
})
我的survey.html有:
< form action =/ survey /method =POST >
< table>
{{formset}}
< / table>
< input type =submitvalue =Submit>
< / form>
哦,来自models.py的ScoreForm和Score的定义是:
class Score(models.Model):
所以,如果上述不清楚,我打算制作的是一个每个电影有一行的表单,每行显示一个标题,并有一个框允许用户输入他们的分数。
movie = models.ForeignKey(Movie)
score = models.IntegerField()
user = models.ForeignKey(User)
class ScoreForm(ModelForm):
class Meta:
model = Score
如果有人能指出我正确的方法,我会非常感谢
解决方案起初,我以为这是formets是什么,但我看不到任何方式自动迭代超过数据库表的内容,以生成表单中的项目,如果你看到我的意思。
你需要得到一个查询您需要将该查询集提供给您的表单作为初始数据。请参阅使用初始数据一个表单代码。
initial = [每个表单一个{dictionaries}列表]
有趣的是,这是模型API通过
值的直接特征
方法。I'm working on my first Django application. In short, what it needs to do is to display a list of film titles, and allow users to give a rating (out of 10) to each film. I've been able to use the {{ form }} and {{ formset }} syntax in a template to produce a form which lets you rate one film at a time, which corresponds to one row in a MySQL table, but how do I produce a form that iterates over all the movie titles in the database and produces a form that lets you rate lots of them at once?
At first, I thought this was what formsets were for, but I can't see any way to automatically iterate over the contents of a database table to produce items to go in the form, if you see what I mean.
Currently, my views.py has this code:
def survey(request): ScoreFormSet = formset_factory(ScoreForm) if request.method == 'POST': formset = ScoreFormSet(request.POST, request.FILES) if formset.is_valid(): return HttpResponseRedirect('/') else: formset = ScoreFormSet() return render_to_response('cf/survey.html', { 'formset':formset, })
And my survey.html has this:
<form action="/survey/" method="POST"> <table> {{ formset }} </table> <input type = "submit" value = "Submit"> </form>
Oh, and the definition of ScoreForm and Score from models.py are:
class Score(models.Model): movie = models.ForeignKey(Movie) score = models.IntegerField() user = models.ForeignKey(User) class ScoreForm(ModelForm): class Meta: model = Score
So, in case the above is not clear, what I'm aiming to produce is a form which has one row per movie, and each row shows a title, and has a box to allow the user to enter their score.
If anyone can point me at the right sort of approach to this, I'd be most grateful.
解决方案"At first, I thought this was what formsets were for, but I can't see any way to automatically iterate over the contents of a database table to produce items to go in the form, if you see what I mean."
You need to get a queryset. And you need to provide that queryset to your form as initial data. See using initial data with a formset for the code.
initial = [ list of { dictionaries }, one per form ]
Interestingly, this is a direct feature of the model API through the
values
method of a queryset.这篇关于Form(或Formset?)来处理Django中的多个表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!