我有一个django应用程序,有几种形式,我正在尝试迁移到ajax。我这样做是因为我想更新我的数据而不刷新我的屏幕。我使用了dajax库并开始将一些views.py代码移动到ajax.py中。
我在views.py中运行良好的代码中得到了“ValueError:解压缩的值太多”。我不确定为什么会发生这种情况或如何解决它。建议?
ajax.py
def send_student_form(request, form):
dajax = Dajax()
#error is here
student = Student.objects.get( form.get('student_id'))
#student = Student() # no ValueError, but it doesn't find my student.
if student:
print "Student Found!"
sForm = StudentProfileForm(request.POST, instance=student)
print student
else:
print "Student Not Found"
sForm = StudentProfileForm(request.POST)
#TODO: new Student
答案 0 :(得分:1)
您需要定义要查询的字段,例如:
# Query by primary key
student = Student.objects.get(pk = form.get('student_id'))
也允许使用非关键字语法,但很难看。非关键字参数是Django查询构建过程中的一个特例,如果您想了解更多信息,请参阅Q
类的构造函数。
student = Student.objects.get(("pk", form.get('student_id')))