• <bdo id='dUtEJ'></bdo><ul id='dUtEJ'></ul>

      1. <small id='dUtEJ'></small><noframes id='dUtEJ'>

        <legend id='dUtEJ'><style id='dUtEJ'><dir id='dUtEJ'><q id='dUtEJ'></q></dir></style></legend>

        <i id='dUtEJ'><tr id='dUtEJ'><dt id='dUtEJ'><q id='dUtEJ'><span id='dUtEJ'><b id='dUtEJ'><form id='dUtEJ'><ins id='dUtEJ'></ins><ul id='dUtEJ'></ul><sub id='dUtEJ'></sub></form><legend id='dUtEJ'></legend><bdo id='dUtEJ'><pre id='dUtEJ'><center id='dUtEJ'></center></pre></bdo></b><th id='dUtEJ'></th></span></q></dt></tr></i><div id='dUtEJ'><tfoot id='dUtEJ'></tfoot><dl id='dUtEJ'><fieldset id='dUtEJ'></fieldset></dl></div>
      2. <tfoot id='dUtEJ'></tfoot>
      3. 模型表单中的 Django 必填字段

        时间:2023-09-03

          <small id='0rJzy'></small><noframes id='0rJzy'>

            <tbody id='0rJzy'></tbody>

                  <bdo id='0rJzy'></bdo><ul id='0rJzy'></ul>
                  <tfoot id='0rJzy'></tfoot>
                  <i id='0rJzy'><tr id='0rJzy'><dt id='0rJzy'><q id='0rJzy'><span id='0rJzy'><b id='0rJzy'><form id='0rJzy'><ins id='0rJzy'></ins><ul id='0rJzy'></ul><sub id='0rJzy'></sub></form><legend id='0rJzy'></legend><bdo id='0rJzy'><pre id='0rJzy'><center id='0rJzy'></center></pre></bdo></b><th id='0rJzy'></th></span></q></dt></tr></i><div id='0rJzy'><tfoot id='0rJzy'></tfoot><dl id='0rJzy'><fieldset id='0rJzy'></fieldset></dl></div>
                • <legend id='0rJzy'><style id='0rJzy'><dir id='0rJzy'><q id='0rJzy'></q></dir></style></legend>
                  本文介绍了模型表单中的 Django 必填字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个表单,当我不想要它们时,会根据需要显示几个字段.这是来自models.py的表格

                  I have a form where a couple of fields are coming out as required when I don't want them too. Here is the form from models.py

                  class CircuitForm(ModelForm):
                      class Meta:
                          model = Circuit
                          exclude = ('lastPaged',)
                      def __init__(self, *args, **kwargs):
                          super(CircuitForm, self).__init__(*args, **kwargs)
                          self.fields['begin'].widget = widgets.AdminSplitDateTime()
                          self.fields['end'].widget = widgets.AdminSplitDateTime()
                  

                  在实际的Circuit模型中,字段是这样定义的:

                  In the actual Circuit model, the fields are defined like this:

                  begin = models.DateTimeField('Start Time', null=True, blank=True)
                  end = models.DateTimeField('Stop Time', null=True, blank=True)
                  

                  我的views.py在这里:

                  My views.py for this is here:

                  def addCircuitForm(request):
                      if request.method == 'POST':
                          form = CircuitForm(request.POST)
                          if form.is_valid():
                              form.save()
                              return HttpResponseRedirect('/sla/all')
                      form = CircuitForm()    
                      return render_to_response('sla/add.html', {'form': form})
                  

                  我该怎么做才能使这两个字段不再是必需的?

                  What can I do so that the two fields aren't required?

                  推荐答案

                  如果您不想修改模型中字段的空白设置(这样做会破坏管理站点中的正常验证),您可以在你的表单类:

                  If you don't want to modify blank setting for your fields inside models (doing so will break normal validation in admin site), you can do the following in your Form class:

                  def __init__(self, *args, **kwargs):
                      super(CircuitForm, self).__init__(*args, **kwargs)
                  
                      for key in self.fields:
                          self.fields[key].required = False 
                  

                  重新定义的构造函数不会损害任何功能.

                  The redefined constructor won't harm any functionality.

                  这篇关于模型表单中的 Django 必填字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PyGame 应用程序中的 SVG 渲染.在 Pygame 2.0 之前,Pygame 不支持 SVG.那你是怎么加载的 下一篇:Django 小部件覆盖模板

                  相关文章

                • <tfoot id='4bvz1'></tfoot>
                  1. <small id='4bvz1'></small><noframes id='4bvz1'>

                      <bdo id='4bvz1'></bdo><ul id='4bvz1'></ul>

                    <legend id='4bvz1'><style id='4bvz1'><dir id='4bvz1'><q id='4bvz1'></q></dir></style></legend>
                    <i id='4bvz1'><tr id='4bvz1'><dt id='4bvz1'><q id='4bvz1'><span id='4bvz1'><b id='4bvz1'><form id='4bvz1'><ins id='4bvz1'></ins><ul id='4bvz1'></ul><sub id='4bvz1'></sub></form><legend id='4bvz1'></legend><bdo id='4bvz1'><pre id='4bvz1'><center id='4bvz1'></center></pre></bdo></b><th id='4bvz1'></th></span></q></dt></tr></i><div id='4bvz1'><tfoot id='4bvz1'></tfoot><dl id='4bvz1'><fieldset id='4bvz1'></fieldset></dl></div>