article/models.py
...
class ArticlePost(models.Model):
...
total_views = models.PositiveIntegerField(default=0)
...
PositiveIntegerField是用于存储正整数的字段
default=0设定初始值从0开始
修改完数据库别忘了要数据迁移 ,否则更改不会生效。
由于新字段设置了初始值,迁移会很顺畅:
(env) E:\django_project\my_blog>python manage.py makemigrations
Migrations for 'article':
article\migrations\0003_articlepost_total_views.py
- Add field total_views to articlepost
Migrations for 'userprofile':
userprofile\migrations\0002_auto_20181227_2041.py
- Alter field avatar on profile
- Alter field user on profile
(env) E:\django_project\my_blog>python manage.py migrate
Operations to perform:
Apply all migrations: admin, article, auth, contenttypes, sessions, userprofile
Running migrations:
Applying article.0003_articlepost_total_views... OK
Applying userprofile.0002_auto_20181227_2041... OK