ここを参照(リンク)
DBに新しくカラムを追加したいと考え、試しに適当にフィールドを追加
class Post(models.Model):title = models.CharField('タイトル', max_length=200)category = models.CharField('カテゴリ', max_length=200)text = models.TextField('本文')date = models.DateTimeField('日付', default=timezone.now)redate = models.DateTimeField('更新日時', auto_now=True)
しかし、エラー出てしまう
It is impossible to add a non-nullable field 'category' to post without specifying a default. This is because the database needs something to populate existing rows.Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit and manually define a default value in models.py.
どうやらnullにできないカラムは追加できないそう
そこでnullでも良いカラムとして追加
class Post(models.Model):title = models.CharField('タイトル', max_length=200)category = models.CharField('カテゴリ', max_length=200, null = True)text = models.TextField('本文')date = models.DateTimeField('日付', default=timezone.now)redate = models.DateTimeField('更新日時', auto_now=True)
% python3 manage.py makemigrations 1.24.1 Migrations for 'blog': blog/migrations/0006_post_category.py - Add field category to post
成功!!!