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

前端 未结 16 1346
北恋
北恋 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:11

    One option is to declare a default value for 'new_field':

    new_field = models.CharField(max_length=140, default='DEFAULT VALUE')
    

    another option is to declare 'new_field' as a nullable field:

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

    If you decide to accept 'new_field' as a nullable field you may want to accept 'no input' as valid input for 'new_field'. Then you have to add the blank=True statement as well:

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

    Even with null=True and/or blank=True you can add a default value if necessary:

    new_field = models.CharField(max_length=140, default='DEFAULT VALUE', blank=True, null=True)
    

提交回复
热议问题