Django:使用实例初始化自定义表单的FormSet [英] Django: Initializing a FormSet of custom forms with instances
问题描述
给出以下模型:
class Graph(models.Model):
owner = models.ForeignKey(User)
def __unicode__(self):
return u'%d' % self.id
class Point(models.Model):
graph = models.ForeignKey(Graph)
date = models.DateField(primary_key = True)
abs = models.FloatField(null = True)
avg = models.FloatField(null = True)
def __unicode__(self):
return u'%s' % self.date
我正在尝试创建一个用于编辑点列表的表单.HTML输入标签需要设置其他属性,因此我使用以下自定义形式:
I am trying to create a form for editing lists of Points. The HTML input tags require additional attributes to be set, so I am using the following custom form:
class PointForm(forms.ModelForm):
graph = forms.ModelChoiceField(queryset = Graph.objects.all(),
widget = forms.HiddenInput())
date = forms.DateField(widget = forms.HiddenInput(), label = 'date')
abs = forms.FloatField(widget = forms.TextInput(
attrs = {'class': 'abs-field'}),
required = False)
class Meta:
model = Point
fields = ('graph', 'date', 'abs') # Other fields are not edited.
def pretty_date(self):
return self.data.strftime('%B')
这时我不知道如何将Point类的实例传递给FormSet:
At this point I do not know how to pass instances of the Point class to a FormSet:
def edit(request):
PointFormSet = forms.formsets.formset_factory(PointForm, extra = 0)
if request.method == 'POST':
return
# Receive 3 points to edit from the database.
graph, res = Graph.objects.get_or_create(id = 1)
one_day = datetime.timedelta(days = 1)
today = datetime.date.today()
do_edit = []
for date in [today - (x * one_day) for x in range(3)]:
point, res = Point.objects.get_or_create(graph = graph, date = date)
do_edit.append(point)
formset = PointFormSet(????) # How is this initialized with the points?
我发现了一种可以起到一定作用的黑客工具,但是稍后在尝试处理生成的POST数据时会导致错误:
I found a hack that somewhat works, but it leads to errors later on when trying to process the resulting POST data:
do_edit = []
for date in [today - (x * one_day) for x in range(3)]:
point, res = Point.objects.get_or_create(graph = graph, date = date)
data = point.__dict__.copy()
data['graph'] = graph
do_edit.append(data)
formset = PointFormSet(initial = do_edit)
这如何正确完成?
作为参考,我的模板如下所示:
For the reference, my template looks like this:
<form action="" method="post">
{{ formset.management_form }}
<table>
<tbody>
{% for form in formset.forms %}
<tr>
<td>{{ form.graph }} {{ form.date }} {{ form.pretty_date }}:</td>
<td width="100%">{{ form.abs }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</form>
推荐答案
诀窍是使用"ModelFormset"而不是仅使用表单集,因为它们允许使用查询集进行初始化.这些文档是此处,您可以做什么在创建模型表单集时提供一个form = *,在实例化表单集时提供queryset = *.form = *争论没有得到充分的记录(必须在代码中进行一些挖掘以确保它确实存在).
The trick is to use a "ModelFormset" instead of just a formset since they allow initialization with a queryset. The docs are here, what you do is provide a form=* when creating the model formset and queryset=* when your instantiating the formset. The form=* arguement is not well documented (had to dig around in the code a little to make sure it is actually there).
def edit(request):
PointFormSet = modelformset_factory(Point, form = PointForm)
qset = Point.objects.all() #or however your getting your Points to modify
formset = PointFormset(queryset = qset)
if request.method == 'POST':
#deal with posting the data
formset = PointFormset(request.POST)
if formset.is_valid():
#if it is not valid then the "errors" will fall through and be returned
formset.save()
return #to your redirect
context_dict = {'formset':formset,
#other context info
}
return render_to_response('your_template.html', context_dict)
因此,代码很容易遍历.如果请求是GET,则将实例化的表单返回给用户.如果请求是POST,并且表单是 not .is_valid()
,则错误掉线"并在同一模板中返回.如果请求是POST且数据有效,那么将保存表单集.
So the code walks through easily. If the request is a GET then the instantiated form is returned to the user. If the request is a POST and the form is not .is_valid()
then the errors "fall through" and are returned in the same template. If the request is a POST and the data is valid then the formset is saved.
希望有帮助.
-将
这篇关于Django:使用实例初始化自定义表单的FormSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!