Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 更改模型后,ModelForm save()不起作用_Django_Django Forms - Fatal编程技术网

Django 更改模型后,ModelForm save()不起作用

Django 更改模型后,ModelForm save()不起作用,django,django-forms,Django,Django Forms,我正在玩jessykate()的django调查,并修改了models.py。现在save()方法不再工作了,我不明白为什么会这样 models.py(见注释#) views.py(原件) forms.py(见注释#) 所以,当我保存一个调查时,我会得到与我所有输入完全相同的页面,而不是确认页面 有什么线索吗?您当前正在修改项目中的第三方代码。你面临的困难是一个快速的教训(我们都学到了)为什么这是一个坏主意™. 通过查看代码,您似乎只想从响应模型中去掉一些字段。解决这个问题的更好办法是编写自己的

我正在玩jessykate()的django调查,并修改了models.py。现在save()方法不再工作了,我不明白为什么会这样

models.py(见注释#)

views.py(原件)

forms.py(见注释#)

所以,当我保存一个调查时,我会得到与我所有输入完全相同的页面,而不是确认页面


有什么线索吗?

您当前正在修改项目中的第三方代码。你面临的困难是一个快速的教训(我们都学到了)为什么这是一个坏主意™. 通过查看代码,您似乎只想从
响应
模型中去掉一些字段。解决这个问题的更好办法是编写自己的
MyResponse
模型并使用它,而不是编辑第三方应用程序的源代码

如果您坚持使用您的修改(不要坚持使用您的修改),那么您需要确定为什么
表单.is\u valid()
False
(这是一个猜测,“save()不起作用”非常模糊,但您没有发布错误回溯,因此我假设没有错误回溯)。您的表单中有错误,如果您访问这些错误:

for field, errors in form.errors.items():
    print field
    for error in errors:
        print error
然后他们会给你一个更好的想法发生了什么


编辑:从您发布的错误中可以看出问题所在,当表单缺少
调查属性时,您正在调用
表单.is\u valid()
,因此
is\u valid()
的计算结果为False,而表单的save()方法甚至从未被调用。在调用
form.is\u valid()
之前,设置
form.survey=survey
,看看会发生什么情况。

看起来您的表单无效,因此当它遇到
if form.is\u valid()
条件时,它会跳过它(插入一个断言以确认这一点),因此甚至不会调用save()方法。如果表单确实无效,您需要检查表单错误以找出原因。问题在于
字段=(“采访者”、“受访者”、“条件”、“评论”)
。如果我将面试官和被面试者添加回models.py,并在forms.py中设置字段,它就会起作用。forms.py中是否需要
字段?“我添加了它,问题就消失了”并不特别意味着问题集中在您添加/删除的行上,它只是一个开始查找的指示器。我想知道表单错误,因为在没有回溯的情况下,它们是关于表单为什么不使用
save()
的最具描述性的信息。我将添加更详细的回答作为答案。不回答以下问题:
本次调查的类别:[u'Test',u'Neuland']问题2此字段为必填字段。问题1此字段为必填字段。面试?此字段为必填字段。调查此字段是必需的。[2014年1月8日04:57:30]“POST/survey/1/HTTP/1.1”200 4211
回答问题:
本次调查的类别:[u'Test',u'Neuland']采访\u uuid此字段为必填字段。调查此字段是必需的。
仍然相同。我以为调查已经通过
form=ResponseForm(request.POST,survey=survey)
def SurveyDetail(request, id):
    survey = Survey.objects.get(id=id)
    category_items = Category.objects.filter(survey=survey)
    categories = [c.name for c in category_items]
    print 'categories for this survey:'
    print categories
    if request.method == 'POST':
        form = ResponseForm(request.POST, survey=survey)
        if form.is_valid():
            response = form.save()
            return HttpResponseRedirect("/confirm/%s" % response.interview_uuid)
    else:
        form = ResponseForm(survey=survey)
        print form
        # TODO sort by category
    return render(request, 'survey.html', {'response_form': form, 'survey': survey, 'categories': categories})
class ResponseForm(models.ModelForm):
    class Meta:
        model = Response    
    #   fields = ('interviewer', 'interviewee', 'conditions', 'comments')

[...]

def save(self, commit=True):
        ''' save the response object '''
        response = super(ResponseForm, self).save(commit=False)
        response.survey = self.survey
        response.interview_uuid = self.uuid
        response.save()

        '''
            create an answer object for each question and associate it with this 
            response.
            '''
        for field_name, field_value in self.cleaned_data.iteritems():
            if field_name.startswith("question_"):
                # warning: this way of extracting the id is very fragile and
                # entirely dependent on the way the question_id is encoded in the
                # field name in the __init__ method of this form class.
                q_id = int(field_name.split("_")[1])
                q = Question.objects.get(pk=q_id)

                if q.question_type == Question.TEXT:
                    a = AnswerText(question = q)
                    a.body = field_value
                elif q.question_type == Question.RADIO:
                    a = AnswerRadio(question = q)   
                    a.body = field_value
                elif q.question_type == Question.SELECT:
                    a = AnswerSelect(question = q)  
                    a.body = field_value
                elif q.question_type == Question.SELECT_MULTIPLE:
                    a = AnswerSelectMultiple(question = q)  
                    a.body = field_value
                elif q.question_type == Question.INTEGER:   
                    a = AnswerInteger(question = q) 
                    a.body = field_value
                print "creating answer to question %d of type %s" % (q_id, a.question.question_type) 
                print a.question.text
                print 'answer value:'
                print field_value
                a.response = response
                a.save()
        return response
for field, errors in form.errors.items():
    print field
    for error in errors:
        print error