一個formset:Django的:初始化的自定義表單與考慮下面的模型的實例
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輸入標籤需要進行設置附加屬性,所以我用下面的自定義窗體:
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類的實例傳遞到表單集中:
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數據時,它會導致錯誤以後:
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)
這是如何正確地做了什麼?
對於參考,我的模板看起來是這樣的:
<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>
Ahhhh,非常感謝你,它的作品非常漂亮。小修正:在驗證POST數據時,初始化如下所示:「PointFormset(request.POST,queryset = qset)」。否則你會得到這個錯誤:「與這個日期點已經存在」。 – knipknap 2010-01-02 20:16:33
doh ...好的......我忘記了在使用model_formset和提供的數據時你需要給查詢集......我從來沒有真正使用過這種用例。 – JudoWill 2010-01-03 00:05:16