[Python3] Django1.7 Models.py update하기.

Category: Python3 | March 21, 2016

models.py파일에 컬럼이 추가 또는 삭제가 되어 업데이트가 발생한 경우.

ex)

  • 추가되기 전
from django.db import models

# Create your models here.
class Article(models.Model):
    name = models.CharField(max_length=50)
    title = models.CharField(max_length=50)
  • 추가된 후
from django.db import models

# Create your models here.
class Article(models.Model):
    name = models.CharField(max_length=50)
    title = models.CharField(max_length=50)
    contents = models.TextField()

makemigration <app -name> 하게 되면 변동이 발생한 내역에 대하여 두 가지를 선택하게 된다.

You are trying to add a non-nullable field 'email' to article without a default;
we can't do that (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)
2) Quit, and let me add a default in models.py

Select an option: 1

위와 같이 1 번을 선택한 후, ' '' ' 값을 입력한다.

Please enter the default value now, as valid Python
The datetime module is available, so you can do e.g. "datetime.date.today()"
>>> ''

$ ./manage.py makemigration 
$ ./manage.py migrate

</code></pre>

</app>