当我更新记录时,在Django

时间:2019-08-23 09:24:36

标签: python django postgresql django-models django-forms

当我尝试使用表单更新Django中的记录时出现错误。我收到一条记录正在更新但删除末行的错误。如何解决此问题?

在Postgresql中更新之前:

enter image description here

在Postgresql中更新后:

enter image description here

在记录更新时,我不想更改行顺序,而是更改记录的行顺序。

查看代码:

from django.shortcuts import render,redirect,get_object_or_404
from Table_definition.forms import TableDefinitionsForms
from Table_definition.models import sysTables

def update_table(request,id):
    process_type = "update"
    table_data = get_object_or_404(sysTables, id = id)
    table_form = TableDefinitionsForms(
        request.POST or None,
        request.FILES or None,
        instance=table_data)

    if table_form.is_valid():
        table_form.save()
        return redirect("Table_definition:table") 

    context = {
        "table_form":table_form,
        "process_type":process_type
    }
    return render(request, 'tableCreate.html', context)

表单代码:

from django import forms
from Table_definition.models import sysTables

class TableDefinitionsForms(forms.ModelForm):
    class Meta:
        model = sysTables
        fields = [
            'name',
            'alias' , 
            'aliasLng1' ,
            'aliasLng2' ,
            'chistory',
            'rhistory',
            'uhistory' ,
            'dhistory' ,
            'log',
        ]
        widgets = {
            'name':forms.TextInput(attrs={'placeholder': '','class':'rc-form-control-input form-control-user'}),
            'alias': forms.TextInput(attrs={'placeholder': '','class':'rc-form-control-input form-control-user'}),
            'aliasLng1': forms.TextInput(attrs={'placeholder': '','class':'rc-form-control-input form-control-user'}),
            'aliasLng2': forms.TextInput(attrs={'placeholder': '','class':'rc-form-control-input form-control-user'}),
            'chistory': forms.CheckboxInput(attrs={'placeholder': '','class':'form-check-input','id':'chistory'}),
            'rhistory': forms.CheckboxInput(attrs={'placeholder': '','class':'form-check-input','id':'rhistory'}),
            'uhistory': forms.CheckboxInput(attrs={'placeholder': '','class':'form-check-input','id':'uhistory'}),
            'dhistory': forms.CheckboxInput(attrs={'placeholder': '','class':'form-check-input','id':'dhistory'}),
            'log': forms.CheckboxInput(attrs={'placeholder': '','class':'form-check-input','id':'log'}),
        }

0 个答案:

没有答案