143

I have this code

subject      = models.ForeignKey(subjects)
location     = models.ForeignKey(location)
publisher    =  models.ForeignKey(publisher)

There its not always possible that I have three values of books. so sometimes if I don't know subject or location, or publisher. Then I want to keep them empty

But if I have then I need select box to select. is it possible like that

Vini.g.fer
  • 11,093
  • 15
  • 55
  • 83
user1
  • 2,069
  • 3
  • 18
  • 18

3 Answers3

257

Sure, just add blank=True, null=True for each field that you want to remain optional like

subject = models.ForeignKey(subjects, blank=True, null=True)
akki
  • 1,649
  • 1
  • 21
  • 34
Abid A
  • 7,230
  • 4
  • 29
  • 32
  • 12
    Are both needed? What would be the downsides of just using null=True ? – Ward Jan 21 '16 at 08:15
  • 16
    @WardC 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 (''). – fang_dejavu Jun 20 '16 at 18:32
  • 8
    @Ward, both are not needed. It is sufficient to just use `null=True`. – Clay Risser May 20 '17 at 22:27
  • 29
    `blank=True` is important for form validation. It will allow an empty string as user input in this field. `null=True` will allow *NULL* value in the database column. – cezar Aug 23 '18 at 10:31
  • Is something else required if you originally didn't have `blank=True, null=True` but do now with the migrations applied? I have this situation and while the schema looks good, the admin form won't allow a blank. Other FKs that allowed blanks originally work though and I don't see the difference. – JFlo Oct 09 '18 at 13:59
  • In Django Rest Framework, it seems you also have to specify required=False in the serializer: https://github.com/encode/django-rest-framework/issues/627. Something like subject=SubjectSerializer(required=False). – Little Brain May 26 '19 at 14:55
  • `blank` is for "blank input from form data e.g. the user could enter nothing in this field" `null` is for "allow to be stored as null (nothing) in the database" – Swift Sep 30 '19 at 14:24
  • Thanks for the tip. For me (Django 3.2.7) it was asked to me to add : missing 1 required positional argument: 'on_delete' – Abpostman1 Mar 22 '22 at 12:32
5

In order to accomplish this, the on_delete argument is necessary along with blank=True and null=True, and it would be better if you do it this way.

subject = models.ForeignKey(subjects, on_delete=models.SET_NULL, blank=True, null=True)
Muhammad Zubair
  • 325
  • 2
  • 13
2

null=True


subject=models.ForeignKey(subjects, on_delete=models.CASCADE, default=None, blank=True, null=True)

Field.null

If True, Django will store empty values as NULL in the database. Default is False.

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. One exception is when a CharField has both unique=True and blank=True set. In this situation, null=True is required to avoid unique constraint violations when saving multiple objects with blank values.

MD SHAYON
  • 7,911
  • 47
  • 35