<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有什么区别?

        时间:2024-04-20
          <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有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:设置计划的工作? 下一篇:让 Django 提供可下载文件

                  相关文章

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

                  2. <small id='aszjJ'></small><noframes id='aszjJ'>

                  3. <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>