Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.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中的错误_Django_Forms_Field - Fatal编程技术网

给予初始价值提升';字段的值是必填的';django中的错误

给予初始价值提升';字段的值是必填的';django中的错误,django,forms,field,Django,Forms,Field,Django表单多次绊倒我 我给了一个ChoiceField(init表单类)初始值 使用上述代码创建的thread\u type表单无效(),因为“此字段(thread\u type)是必需的” -编辑- 找到了修复方法,但它仍然让我有点困惑 我的模板中有一个代码 {% if request.user.is_administrator() %} <div class="select-post-type-div">

Django表单多次绊倒我

我给了一个ChoiceField(init表单类)初始值

使用上述代码创建的
thread\u type
表单无效(),因为“此字段(thread\u type)是必需的”

-编辑-
找到了修复方法,但它仍然让我有点困惑

我的模板中有一个代码

   {% if request.user.is_administrator() %}
      <div class="select-post-type-div">                                                                                                                                                                                                                                        
        {{form.thread_type}}                                                                                                                                                                                                                                                    
      </div> 
   {% endif %}
我不明白为什么通过下面的公式给出初始值(同上)是不够的

self.fields['thread_type'] = forms.ChoiceField(choices=choices, 
        widget=forms.Select, 
        initial=thread_type)  
并且,在
request.POST中包含
thread\u type
变量允许表单通过is\u valid()检查

表单类代码如下所示

class EditQuestionForm(PostAsSomeoneForm, PostPrivatelyForm):
    title = TitleField()
    tags = TagNamesField()


    #some more fields.. but removed for brevity, thread_type isn't defined here 

    def __init__(self, *args, **kwargs):
        """populate EditQuestionForm with initial data"""
        self.question = kwargs.pop('question')
        self.user = kwargs.pop('user')#preserve for superclass                                                                                                                                                                                                                  
        thread_type = kwargs.pop('thread_type', self.question.thread.thread_type)
        revision = kwargs.pop('revision')
        super(EditQuestionForm, self).__init__(*args, **kwargs)
        #it is important to add this field dynamically     

        self.fields['thread_type'] = forms.ChoiceField(choices=choices, widget=forms.Select, initial=thread_type)

与其动态添加此字段,不如在类中适当地定义它:

class EditQuestionForm(PostAsSomeoneForm, PostPrivatelyForm):
    title = TitleField()
    tags = TagNamesField()
    thread_type = forms.ChoiceField(choices=choices, widget=forms.Select)
创建表单实例时,如果需要,请设置初始值:

form = EditQuestionForm(initial={'tread_type': thread_type})
如果您不需要此字段,请将其删除:

class EditQuestionForm(PostAsSomeoneForm, PostPrivatelyForm):
    def __init__(self, *args, **kwargs):
        super(EditQuestionForm, self).__init__(*args, **kwargs)
        if some_condition:
            del self.fields['thread_type']
保存表单时,请选中:

thread_type = self.cleaned_data['thread_type'] if 'thread_type' in self.cleaned_data  else None

这种方法对我来说总是很好。

你能展示更多的代码吗?你们什么时候开这条线?谢谢。用相关代码更新了问题。当然可以调整你的方式。如果可能的话,我更喜欢在Form类中设置首字母缩写。因为使用什么作为初始值有时会占用相当多的代码,我不希望在我的视图中使用它…您仍然可以在类内设置初始值,只是出于某种原因,我认为您需要在每次我可以在类内设置初始值时动态设置它,但是.is_valid()将引发错误,如果该字段是必需的,并且给定了初始值,但没有给定值,那么在您删除它之后也会发生这种情况?当我在GET请求中以这种方式创建表单,并呈现表单,然后用户使用POST提交表单时,会发生这种情况。POST时未设置带首字母的字段。
class EditQuestionForm(PostAsSomeoneForm, PostPrivatelyForm):
    def __init__(self, *args, **kwargs):
        super(EditQuestionForm, self).__init__(*args, **kwargs)
        if some_condition:
            del self.fields['thread_type']
thread_type = self.cleaned_data['thread_type'] if 'thread_type' in self.cleaned_data  else None