Applion 005 | null = True , blank = True 참고

Yunny.Log ·2022년 2월 3일
0

ToyProject

목록 보기
9/13
post-thumbnail

https://stackoverflow.com/questions/8609192/what-is-the-difference-between-null-true-and-blank-true-in-django

null = True
데이터베이스에 존재하지 않아도 된다, 필수로 요구되는 값 아님
null = False
반드시 데이터베이스에 존재해야 하는 값 , None 같은 경우 허용 x
blank = True
빈칸으로 놔둬도 됨, 데이터베이스에 상관 안끼치는 속성
blank = False
빈칸이면 절대 안된다

1)

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 ('').

blank has no effect on the database, and null controls whether the database column allows NULL values.

2)

null=False, blank=False:

  • This is the default configuration and means that the value is required in all circumstances.

null=True, blank=True:

  • This means that the field is optional in all circumstances. (As noted below, though, this is not the recommended way to make string-based fields optional.)

null=False, blank=True:

  • This means that the form doesn't require a value but the database does. There are a number of use cases for this:
    • The most common use is for optional string-based fields. As noted in the documentation, the Django idiom is to use the empty string to indicate a missing value. If NULL was also allowed you would end up with two different ways to indicate a missing value.
    • Another common situation is that you want to calculate one field automatically based on the value of another (in your save() method, say). You don't want the user to provide the value in a form (hence blank=True), but you do want the database to enforce that a value is always provided (null=False).
    • Another use is when you want to indicate that a ManyToManyField is optional. Because this field is implemented as a separate table rather than a database column, null is meaningless. The value of blank will still affect forms, though, controlling whether or not validation will succeed when there are no relations.

null=True, blank=False:

  • This means that the form requires a value but the database doesn't. This may be the most infrequently used configuration, but there are some use cases for it:
    • It's perfectly reasonable to require your users to always include a value even if it's not actually required by your business logic. After all, forms are only one way of adding and editing data. You may have code that is generating data which doesn't need the same stringent validation that you want to require of a human editor.
    • Another use case that I've seen is when you have a ForeignKey for which you don't wish to allow cascade deletion. That is, in normal use the relation should always be there (blank=False), but if the thing it points to happens to be deleted, you don't want this object to be deleted too. In that case you can use null=True and on_delete=models.SET_NULL to implement a simple kind of soft deletion.

0개의 댓글