【发布时间】:2020-01-15 21:10:16
【问题描述】:
我在模型表单中创建了一个自定义字段,我希望使用该字段使用户能够输入多个输入实例。例如,自定义字段是对应于多个日期的一系列值。提交表单后,我想将所有这些值合并到一个字符串中,并将它们存储在单个数据库字段中。请参阅下面的代码。当我向输入添加多个值时,cleaned_data 字段为空。我想我需要在将输入数据传递给cleaned_data之前访问它,但我不确定如何。任何援助将不胜感激。
#forms.py
class UpdateModelForm(forms.ModelForm):
custom_field = forms.CharField()
class Meta:
model = Model
def clean_custom_field(self):
custom_field = self.cleaned_data['custom_field']
return custom_field
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
#I've tried to access the input fields here without success
#views.py
class ModelView(UpdateView):
model = Model
form_class = UpdateModelForm
template_name = 'model_edit.html'
def form_valid(self, form):
#not sure what to put here
#model_edit.py
<body>
<form action="" method="post">{% csrf_token %}
<table>
<thead>
<tr>
<th>Amount</th>
</thead>
<tbody>
{% for i in '12345'|make_list %}
<tr>
<td>
{{form.custom_field}}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</form>
</body>
【问题讨论】:
标签: django django-forms