6

From: https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.Field.null

Avoid using null on string-based fields such as CharField and TextField. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL.

So What is better on ForeignKey Field? ForeignKey field is not string-based field, isn't it?

I wonder what is convention for ForeignKey in django.

null=True is better or blank=True is better? In performance, convenience or somewhat anything.

touchingtwist
  • 1,930
  • 4
  • 23
  • 38
  • You may refer this : https://stackoverflow.com/questions/8609192/differentiate-null-true-blank-true-in-django?rq=1 – Storm Apr 10 '18 at 07:25

2 Answers2

8

It's different. See docs:

Note that this is different than null. null is purely database-related, whereas blank is validation-related. If a field has blank=True, form validation will allow entry of an empty value. If a field has blank=False, the field will be required.

So for ForeignKey you need to set both: null=True, blank=True.

neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100
6

These are used for different purposes:

  • Null = True: It used if you want to make your column in the database as nullable
  • blank = True: It does not effect in database column. This column still be not nullable (which is default), but in html form the input field can remain empty.
Mahedi Sabuj
  • 2,894
  • 3
  • 14
  • 27