• <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 必填字段

        Django required field in model form(模型表单中的 Django 必填字段)

          <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 必填字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                    • <tfoot id='4bvz1'></tfoot>
                          <tbody id='4bvz1'></tbody>
                      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>