[Answer]-Django: ForeignKey needs a default specified,what's that?

1👍

This problem arises when a foreign key is added to a model, and the model is migrated using south.

Most of the time you do not want to have any null ForeignKey.
Personally, if I ever need to add such a foreign key, I assign it to a default User (mostly the root superuser).

  1. Add the ForeignKey field as you have done it and do ./manage.py schemamigrate app. South gives an option to specifiy the default on the command line. In this case, your default would be User.objects.get(username="root")

  2. Otherwise, if you are fine with having null foreign keys, then following definition should work.

    class Question(models.Model):
        q_user = models.ForeignKey(User, null=True, blank=True, default=None)
    

Leave a comment