In Django 1.4, do Form.has_changed() and Form.changed_data, which are undocumented, work as expected?

前端 未结 3 660
天命终不由人
天命终不由人 2020-12-08 16:01

From the source code, you can see that Django 1.4\'s Form class has a has_changed() method and changed_data property which seem rather

3条回答
  •  独厮守ぢ
    2020-12-08 16:41

    Here is a piece of code that does what @Ghopper21 is suggesting. To get the initial data, I use the values() function on the QueryDict. It returns a dictionary containing the data belonging to the object. An important caveat is that I haven't checked how it handles foreign key references.

    saveStudentView(request,studentID):
        existingData = Student.objects.filter(id=paperID).values()[0]
        form = StudentForm(request.POST)
        if form.is_valid():
            form = StudentForm(request.POST, initial=existingData)
            if not form.has_changed():
                //Tell the user nothing has changed
            else:
                //Do other stuff
    

提交回复
热议问题