How to make a Django ForeignKey optional

Looks like you need to have both blank=True and null=True to make a ForeignKey optional in a django model…


  • Italo Maia

    Reply Reply September 3, 2007

    Thanks ; )

  • Paul Holt

    Reply Reply April 12, 2008

    Thanks Rachel

  • Beau

    Reply Reply April 16, 2008

    Rachel, pretty awesome tip, thanks!!

  • stretch

    Reply Reply April 22, 2008

    Thanks, that just helped me a bunch!

    I also noticed I had to redefine the foreign key column in my database (MySQL) to accept NULL values, since it had already been created before I added null=True:

    alter table articles_article change category_id category_id integer NULL;

  • Vinay

    Reply Reply January 5, 2009

    Awesome, I was looking exactly for how to do this, and found this blog entry. It worked like a charm. Thanks a ton.

  • Steve Androulakis

    Reply Reply February 13, 2009

    Thanks for this! Helped a lot.

  • Brenton

    Reply Reply April 14, 2009

    Thanks Rachel, that was annoying me to no end!

  • Dave

    Reply Reply May 20, 2009

    Me too, saved! Thanks!

  • Michael

    Reply Reply June 22, 2009

    Thanks a bunch, Stretch was what I needed

  • Harshad

    Reply Reply July 27, 2009

    Thanks :-)

  • Pablo

    Reply Reply August 10, 2009

    Its not working for me, i still get an integrity error

  • ???

    Reply Reply August 15, 2010

    syncdb again ^^

  • Sebastian

    Reply Reply December 12, 2010

    Great tip!

  • daijones

    Reply Reply November 4, 2011

    Just when you need the information quickly without too much waffle, up pops a blog like this and does just the job – many thanks! Stretch’s comment helped as well, since I had already created the table without the null-accepting columns.

  • Carlos

    Reply Reply December 18, 2011

    Thanks. Also if synchdb doesn’t work you should drop all those tables by doing a python.exe sqlclear appname > droptables.sql

    shell> mysql -u userid -p DBName < droptables.sql

  • aman

    Reply Reply April 15, 2012

    just in time. thanks for the tip

  • Moritz Krog

    Reply Reply October 10, 2012

    As people still seem to use this rather old blog post, let me clarify something:

    null=True refers to the database representation of the model.
    blank=True refers to the behavior of the field in ModelForms.

    So, while null=True means that you the database accept null-values for this column, form validation will still fail. Separating form and model behavior is necessary because you might to do something like default=’something’ which is only used, when the value from the form is None.

    Anyway, setting null=True is enough to get rid of the IntegrityError as of 1.4 at least.

  • Mathias Heinze

    Reply Reply August 20, 2014

    for updating django databases use the migration tool south. documentation:

    3) generate an initial migration scheme for your unchanged app
    2) then change your definitions in
    3) use south to pass the model changes to your database

Leave A Response

* Denotes Required Field