无法编辑模型,因为编辑时已经存在

时间:2019-06-10 22:53:01

标签: python django bootstrap-4

我正在使用Bootstrap在Django中构建数据库,并且我有要编辑的表单,当我尝试在我的编辑表单中修改表单的值时,我不能这样做,因为“模型已经存在”

我在此处发送ID,删除有效,如果我尝试更改其他值,“ Curso-alumno”也是关联类,“ Profesor”是其他模型

             {% if encuestas %}
            <ul>
            {% for c in encuestas %}
            <th>{{ c.Nota }}</th>
            <th>{{ c.Profesor }}</th>
            <th>{{ c.Curso_alumno }}</th>
             <th><a class="btn btn-danger" href="{% url 'encuesta_delete' c.id %}">Eliminar</a>
            <a class="btn btn-primary" href="{% url 'encuesta_edit' c.id %}">Editar</a></th>
               </tr>
               </tr>

                <tr>
        {% endfor %}

我尝试修改表单,但是无法正常工作。

views.py

class encuesta_edit(UpdateView):
    model = Encuesta
    form_class = tfg_EncuestaForm
    template_name = 'encuesta.html'
    success_url = reverse_lazy('mostrar_encuestas')

forms.py

class tfg_EncuestaForm(forms.ModelForm):
    class Meta:
        model = Encuesta
        fields = ['id', 'Nota', 'Profesor', 'Curso_alumno']

        labels = {
            'id': 'id',
            'Nota': 'Nota',
            'Profesor': 'Profesor',
            'Curso_alumno': 'Curso_alumno',
        }

        widgets = {
            'id': forms.NumberInput(attrs={'class': 'form-control'}),
            'Nota': forms.NumberInput(attrs={'class': 'form-control'}),
            'Profesor': forms.Select(attrs={'class': 'form-control'}),
            'Curso_alumno': forms.Select(attrs={'class': 'form-control'}),
        }

我希望能够无错误地修改mi字段“ Nota”。

0 个答案:

没有答案
相关问题