[Django] - Blank or Null?
장고 모델에서 저장이 될 때
장고에서 해당 모델 object를 저장 할 때 2가지 과정을 거친다.
첫 번째는 validator(controlled by blank) 즉 검증에서 가능한지 파악하고
두 번째는 Database limitation(controlled by null) 데이터베이스에 넣을 때이다.
예
만약에 default=5, blank=True, null=False라고 하면
값(blank=True)을 주지 않아도 통과하게 될 것이다. 왜냐하면 blank=True 때문에 validation을 넘어가게 되고 , default value가 5이기 때문에 db에는 None 대신에 5가 저장될 것이기 때문이다.
하지만 만약에 default value가 없이, blank=True 그리고 null=False만 있다고 하면 통과 할 수 없다. validation은 통과하지만 null을 DB에 저장하려고 시도할 때 null=False이므로 저장할 수가 없다.
따라서 기본적인 옵션을 만들때는
default= something , blank=True, null=False
blank=True, null=True
위의 2가지 조건 중에 한개를 선택해주어야지만 저장이 될 수 있다.
Null and Blank
Django models API offers two similar options that usually cause confusion on many developers: null and blank.
Both do almost the same thing, as the name suggests, but here is the difference:
Null
: It is database-related. Defines if a given database column will accept null values or not.Blank
: It is validation-related. It will be used during forms validation, when calling form.is_valid().
That being said, it is perfectly fine to have a field with null=True
and blank=False
. Meaning on the database level the field can be NULL
, but in the application level it is a required field.
Now, where most developers get it wrong: Defining null=True
for string-based fields such as CharField and TextField. Avoid doing that. Otherwise, you will end up having two possible values for “no data”, that is: None and an empty string. Having two possible values for “no data” is redundant. The Django convention is to use the empty string, not NULL
.
So, if you want a string-based model field to be “nullable”, prefer doing that:
class Person(models.Model):
name = models.CharField(max_length=255) # Mandatory
bio = models.TextField(max_length=500, blank=True) # Optional (don't put null=True)
birth_date = models.DateField(null=True, blank=True) # Optional (here you may add null=True)
The default values of Null
and Blank
are False.
Also there is a special case, when you need to accept NULL
values for a BooleanField, use NullBooleanField
instead.
There are four ways to use these attributes:
# 1. Requires both
Name = models.charField( max_length=10, null=False, blank=False)
# 2. Requires DB value
Name = models.charField( max_length=10, null=False, blank=True)
# 3. Requires modal value
Name = models.charField( max_length=10, null=True, blank=False)
# 4. Requires none
Name = models.charField( max_length=10, null=True, blank=True)
1.) Null=False, Blank=False
This is the default condition for modal fields. It means the value is required both in DB and in models.
2.) Null=False, Blank=True
This is a condition where forms don't require a value, but the field does.
Let's take an example: If your model has a field whose value is getting calculated from other models of the field. Then you would want to keep Blank=True
to not accept values from the user, and Null=False
to enforce value is always provided.
3.) Null=True, Black=False
This is a condition where form requires a value, but DB doesn't. This is not a very commonly used configuration.
4.) Null=True, Black=True
This is a condition, where form doesn't require a value, and DB doesn't require a value as well. This is pretty self explanatory since the former is reflecting with later.
Source
https://simpleisbetterthancomplex.com/tips/2016/07/25/django-tip-8-blank-or-null.html
https://dev.to/anubhavitis/django-null-true-or-blank-true-or-both-30ed
https://daeguowl.tistory.com/64
Author And Source
이 문제에 관하여([Django] - Blank or Null?), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@xxhaileypark/Django-Blank-or-Null저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)