<tfoot id='f6zwH'></tfoot>

      <bdo id='f6zwH'></bdo><ul id='f6zwH'></ul>
    <i id='f6zwH'><tr id='f6zwH'><dt id='f6zwH'><q id='f6zwH'><span id='f6zwH'><b id='f6zwH'><form id='f6zwH'><ins id='f6zwH'></ins><ul id='f6zwH'></ul><sub id='f6zwH'></sub></form><legend id='f6zwH'></legend><bdo id='f6zwH'><pre id='f6zwH'><center id='f6zwH'></center></pre></bdo></b><th id='f6zwH'></th></span></q></dt></tr></i><div id='f6zwH'><tfoot id='f6zwH'></tfoot><dl id='f6zwH'><fieldset id='f6zwH'></fieldset></dl></div>
  • <small id='f6zwH'></small><noframes id='f6zwH'>

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

      1. Django中null=True和blank=True有什么区别?

        What is the difference between null=True and blank=True in Django?(Django中null=True和blank=True有什么区别?)
          <bdo id='k290x'></bdo><ul id='k290x'></ul>

          <tfoot id='k290x'></tfoot>

              <tbody id='k290x'></tbody>
          • <small id='k290x'></small><noframes id='k290x'>

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

                  <i id='k290x'><tr id='k290x'><dt id='k290x'><q id='k290x'><span id='k290x'><b id='k290x'><form id='k290x'><ins id='k290x'></ins><ul id='k290x'></ul><sub id='k290x'></sub></form><legend id='k290x'></legend><bdo id='k290x'><pre id='k290x'><center id='k290x'></center></pre></bdo></b><th id='k290x'></th></span></q></dt></tr></i><div id='k290x'><tfoot id='k290x'></tfoot><dl id='k290x'><fieldset id='k290x'></fieldset></dl></div>
                  本文介绍了Django中null=True和blank=True有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  When we add a database field in django we generally write:

                  models.CharField(max_length=100, null=True, blank=True)
                  

                  The same is done with ForeignKey, DecimalField etc. What is the basic difference in having

                  1. null=True only
                  2. blank=True only
                  3. null=True, blank=True

                  in respect to different (CharField, ForeignKey, ManyToManyField, DateTimeField) fields. What are the advantages/disadvantages of using 1/2/3?

                  解决方案

                  null=True sets NULL (versus NOT NULL) on the column in your DB. Blank values for Django field types such as DateTimeField or ForeignKey will be stored as NULL in the DB.

                  blank determines whether the field will be required in forms. This includes the admin and your custom forms. If blank=True then the field will not be required, whereas if it's False the field cannot be blank.

                  The combo of the two is so frequent because typically if you're going to allow a field to be blank in your form, you're going to also need your database to allow NULL values for that field. The exception is CharFields and TextFields, which in Django are never saved as NULL. Blank values are stored in the DB as an empty string ('').

                  A few examples:

                  models.DateTimeField(blank=True) # raises IntegrityError if blank
                  
                  models.DateTimeField(null=True) # NULL allowed, but must be filled out in a form
                  

                  Obviously, Those two options don't make logical sense to use (though there might be a use case for null=True, blank=False if you want a field to always be required in forms, optional when dealing with an object through something like the shell.)

                  models.CharField(blank=True) # No problem, blank is stored as ''
                  
                  models.CharField(null=True) # NULL allowed, but will never be set as NULL
                  

                  CHAR and TEXT types are never saved as NULL by Django, so null=True is unnecessary. However, you can manually set one of these fields to None to force set it as NULL. If you have a scenario where that might be necessary, you should still include null=True.

                  这篇关于Django中null=True和blank=True有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

                    <tfoot id='aszjJ'></tfoot>
                    • <legend id='aszjJ'><style id='aszjJ'><dir id='aszjJ'><q id='aszjJ'></q></dir></style></legend>

                    • <small id='aszjJ'></small><noframes id='aszjJ'>

                          <tbody id='aszjJ'></tbody>

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