django修改表的结构,加入DateField类型字段

原先mysql数据库表的样子是这样的:


图片.png

django models.py里面关于该表结构是这样的:

class TopQuery(models.Model):
    query = models.CharField(max_length=200)
    # 数量
    counts = models.IntegerField()

    def __unicode__(self):
        # 在Python3中使用 def __str__(self):
        return '%s_%s' % (self.query, self.counts)

现在需要加入一个字段:

# 创建日期
    create_time = models.DateField(auto_now_add=True)

运行python manage.py makemigrations, 报错提示:

You are trying to add a non-nullable field 'create_time' to topquery 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 with a null value for this column)
 2) Quit, and let me add a default in models.py
Select an option:

解决办法:
选择1,然后下一步提示符后面回车:

Select an option: 1
Please enter the default value now, as valid Python
You can accept the default 'timezone.now' by pressing 'Enter' or you can provide another value.
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now
Type 'exit' to exit this prompt
[default: timezone.now] >>> 
Migrations for 'searchlog':
  searchlog/migrations/0002_auto_20180408_0939.py:
    - Add field create_time to topquery
    - Add field create_time to topquerybyfrom

之后运行python manage.py migrate即可。

你可能感兴趣的:(django修改表的结构,加入DateField类型字段)