You are trying to add a non-nullable field 'new_field' to userprofile without a default

前端 未结 16 1308
北恋
北恋 2020-11-30 19:27

I know that from Django 1.7 I don\'t need to use South or any other migration system, so I am just using simple command python manage.py makemigrations

相关标签:
16条回答
  • 2020-11-30 20:12

    In case anyone is setting a ForeignKey, you can just allow nullable fields without setting a default:

    new_field = models.ForeignKey(model, null=True)
    

    If you already have data stored within the database, you can also set a default value:

    new_field = models.ForeignKey(model, default=<existing model id here>)
    
    0 讨论(0)
  • 2020-11-30 20:12

    If the SSH it gives you 2 options, choose number 1, and put "None". Just that...for the moment.

    0 讨论(0)
  • 2020-11-30 20:14

    In models.py

    class UserProfile(models.Model): user = models.OneToOneField(User) website = models.URLField(blank=True) new_field = models.CharField(max_length=140, default="some_value")

    You need to add some values as default.

    0 讨论(0)
  • 2020-11-30 20:16

    You need to provide a default value:

    new_field = models.CharField(max_length=140, default='SOME STRING')
    
    0 讨论(0)
  • 2020-11-30 20:17

    I was early in my development cycle, so this may not work for everyone (but I don't see why it wouldn't).

    I added blank=True, null=True to the columns where I was getting the error. Then I ran the python manage.py makemigrations command.

    Immediately after running this command (and before running python manage.py migrate), I removed the blank=True, null=True from all the columns. Then I ran python manage.py makemigrations again. I was given an option to just change the columns myself, which I selected.

    Then I ran python manage.py migrate and everything worked well!

    0 讨论(0)
  • 2020-11-30 20:18

    In new_file add the boolean property null.

    new_field = models.CharField(max_length=140, null=True)
    

    after you run a ./manage.py syncdb for refresh the DB. and finally you run ./manage.py makemigrations and ./manage.py migrate

    0 讨论(0)
提交回复
热议问题