如何编辑数据而不删除以前添加的内容。在我的情况下,我想使用看起来像这样的form.Form
class SecondForm(forms.Form):
data_1 = forms.ChoiceField(choices=CHOICES, label="")
data_2 = forms.ChoiceField(choices=CHOICES, label="")
data_3 = forms.ChoiceField(choices=CHOICES, label="")
在我的views.py
id = #some object number
if request.method =="POST":
second_form = SecondForm(request.POST)
if second_form.is_valid():
cd = second_form.cleaned_data
object = Valuation(pk=object_id)
object.data_1 = cd['data_1']
object.data_2 = cd['data_2']
object.data_3 = cd['data_3']
object.save()
return HttpResponseRedirect(reverse('app:valuation_third', args=[forwarding]))
else:
second_form = SecondForm()
在这种情况下,如果我的模型如下所示:
class Valuation(models.Model):
data_added_before = models.CharField(max_length=50, blank=True)
data_1 = models.CharField(max_length=50, blank=True)
data_2 = models.CharField(max_length=50, blank=True)
data_3 = models.CharField(max_length=50, blank=True)
每次清除字段“ data_added_before”时,都会对其进行清理。如何避免。因此,保留了该字段前面添加的信息。并将当前表单中的新数据添加到现有数据中。
Tempate.html
{% load crispy_forms_tags %}
<form action="." method="post">
<div class="">
<h2 class="h3 text-primary font-weight-normal mb-4">More information></h2>
</div>
{% csrf_token %}
<p>data_1 name</p>
{{ second_form.data_1|as_crispy_field }}
<p>data_2 name</p>
{{ second_form.data_2|as_crispy_field }}
<p>data_3 name</p>
{{ second_form.data_3|as_crispy_field }}
<div class="form-row">
<div class="form-group col-md-6 mb-0">
<form>
<input class="btn btn-text btn-text-secondary" type="button" value="before" onclick="history.back()">
</form>
</div>
<div class="form-group col-md-6 mb-0">
<div class="text-right">
<button type="submit" class="btn btn-primary">next <i class="fas fa-angle-right fa-lg"></i></button>
</div>
</div>
</div>
</form>