django保存表单 - 覆盖旧行

时间:2015-12-11 09:28:45

标签: python django database

我有一个表单可以向我的数据库添加新数据,但是这个表单会覆盖表格中的现有数据。我想添加一个新的元组并在我的数据库表中保留旧元组

forms.py

class StudentForm(forms.ModelForm):
    class Meta:
        model = student
        fields = ('First_Name', 'Last_Name', 'Birthday', 'Phone', 'Mobile', 'STNO',
          'Father_Name', 'Father_Job', 'Father_Phone', 'ID_Code',
          'National_ID', 'Address', 'Study_Field', 'Probation')

views.py

def add_student(request):
    if request.method == "POST":
        form = StudentForm(request.POST)
        if form.is_valid():
            new_student = form.save(commit=True)
            new_student.author = request.user
            new_student.save()
            return redirect('../')
    else:
        form = StudentForm()
    return render(request, 'school_manager/students/new_student.html', {'form': form})

models.py

class student(models.Model):
    id = models.IntegerField(default=1,null=False,primary_key=True)
    First_Name = models.CharField("First Name ", max_length=100,null=True)
    Last_Name = models.CharField("Last Name ",max_length=100,null=True)
    Birthday = models.CharField("Birthday ",max_length=10,null=True)
    Phone = models.CharField("Phone ",max_length=20,null=True)
    Mobile = models.CharField("Mobile ",max_length=20,null=True)
    STNO = models.CharField("STNO ",max_length=10,null=True)
    Father_Name = models.CharField("Father Name ",max_length=100,null=True)
    Father_Job = models.CharField("Father Job ",max_length=100,null=True)
    Father_Phone = models.CharField("Father Phone ",max_length=20,null=True)
    ID_Code = models.CharField("ID Code ",max_length=10,null=True)
    National_ID = models.CharField("National ID ",max_length=10,null=True)
    Address = models.CharField("Address ",max_length=200,null=True)
    Study_Field = models.CharField("Study Field ",max_length=100,null=True)
    Probation = models.BooleanField("Probation ",default=True)
    def __STR__ (self):
        return self.STNO

2 个答案:

答案 0 :(得分:4)

模型相关。看这个:

DECLARE GetDatabases CURSOR
FOR
    SELECT name
    FROM sys.databases
OPEN GetDatabases
DECLARE @DBName NVARCHAR(100)
DECLARE @cmd NVARCHAR(Max)

FETCH NEXT
FROM GetDatabases
INTO @DBName

WHILE @@FETCH_STATUS = 0
BEGIN
    set @cmd='use ' + @DBName
    print @cmd
    exec sp_executesql @cmd
     FETCH NEXT
     FROM GetDatabases
     INTO @DBName
        DECLARE AutoProc CURSOR
        FOR
            SELECT TABLE_SCHEMA,TABLE_NAME
            FROM INFORMATION_SCHEMA.TABLES
            WHERE TABLE_TYPE='BASE TABLE'
        OPEN AutoProc
        DECLARE @TableName NVARCHAR(100)
        DECLARE @TableSchema NVARCHAR(100)

        FETCH NEXT
        FROM AutoProc
        INTO @TableSchema,@TableName

        WHILE @@FETCH_STATUS = 0
        BEGIN
        IF EXISTS(SELECT * FROM sys.objects WHERE type = 'P' AND OBJECT_ID = OBJECT_ID('@TableName'))
            exec('ALTER PROCEDURE USP_SELECT_'+@TableName+' AS
            BEGIN
            SELECT *
            FROM '+@TableSchema+'.'+@TableName+'
            END ;')
        IF NOT EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND OBJECT_ID = OBJECT_ID('@TableName'))
            exec('CREATE PROCEDURE USP_SELECT_'+@TableName+' AS
            BEGIN
            SELECT *
            FROM '+@TableSchema+'.'+@TableName+'
            END ;')
             FETCH NEXT
             FROM AutoProc
             INTO @TableSchema,@TableName
        END 
        CLOSE AutoProc
        DEALLOCATE AutoProc
END 
CLOSE GetDatabases
DEALLOCATE GetDatabases

您始终创建学生编号class student(models.Model): id = models.IntegerField(default=1,null=False,primary_key=True)

您应该为每个学生手动设置一个新的1

id

另一种方法是将类型def add_student(request): if request.method == "POST": form = StudentForm(request.POST) if form.is_valid(): new_student = form.save(commit=False) id = a_function_to_take_another_id_for_a_student() new_student.id = id new_student = form.save(commit=True) new_student.author = request.user 更改为自动增量字段:

id

答案 1 :(得分:1)

问题出在您的模型中。你明确地设置了ID字段,如下所示:

    id = models.IntegerField(default=1,null=False,primary_key=True)

但是你没有把它设置成形式。这样,django每次都会创建ID为1的行(因为这是默认值)。

当您使用已存在于数据库中的ID创建字段时,django将更新现有字段,因为它无法创建新字段(不能有多个具有相同主键的字段)。

您应该将ID的类型更改为AutoField或删除它的定义,然后django将创建一个AutoField的默认ID字段。